|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-06-28 22:08 UTC] eric at wepay dot com
Description:
------------
If a namespaced class extends a non-namespaced abstract class, typehinted method
parameters are not checked correctly by the interpreter. The code sample shows
this best, but basically the child class implementing the abstract methods isn't
correctly checking how namespaces will apply to typehinted parameters as they were
declared in the base class. While the code aesthetically looks the same, the
parser (as expected) interprets the typehints in the namespaced class to be based
on the current namespace when not explicity specified, but this unintentionally
changes the signature of the methods from that of the abstract class.
Test script:
---------------
========================base.php
<?php
class Payment {}
abstract class CreditCardProcessor {
abstract function AuthorizePayment(Payment $p);
}
========================run.php
<?php
namespace processors\credit_card;
require_once 'base.php';
class ipc extends \CreditCardProcessor {
function AuthorizePayment(Payment $p) {
}
}
$p = new \Payment;
$ipc = new ipc;
$ipc->AuthorizePayment($p);
// note that if processors\credit_card\ipc::AuthorizePayment() is declared as (\Payment $p) instead of (Payment $p), everything behaves as expected since the base class is actually expecting a \Payment object
Expected result:
----------------
PHP Fatal error: Declaration of processors\credit_card\ipc::AuthorizePayment()
must be compatible with that of CreditCardProcessor::AuthorizePayment() in
/Users/eric/dev/run.php on line 5
Actual result:
--------------
PHP Catchable fatal error: Argument 1 passed to
processors\credit_card\ipc::AuthorizePayment() must be an instance of
processors\credit_card\Payment_Model, instance of Payment_Model given, called in
/Users/eric/dev/run.php on line 12 and defined in /Users/eric/dev/run.php on line
6
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 22:00:01 2025 UTC |
zend_compile.c:2997 } else if (strchr(proto_class_name, '\\') != NULL || (colon = zend_memrchr(fe_class_name, '\\', fe_class_name_len)) == NULL || strcasecmp(colon+1, proto_class_name) != 0) { //this line thanks