Bug #62441 [Ver->Csd]: Incorrect strong typing in namespaced child classes

From: Date: Fri, 28 Aug 2015 17:00:34 +0000
Subject: Bug #62441 [Ver->Csd]: Incorrect strong typing in namespaced child classes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195599@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62441&edit=1 ID: 62441 Updated by: nikic@php.net Reported by: eric at wepay dot com Summary: Incorrect strong typing in namespaced child classes -Status: Verified +Status: Closed Type: Bug Package: Class/Object related Operating System: OS X, CentOS PHP Version: 5.3.14 Block user comment: N Private report: N New Comment: Automatic comment on behalf of nikic Revision: http://git.php.net/?p=php-src.git;a=commit;h=ccec609b82b69ef96bc52f7c563c7eb8a68f99a0 Log: Fix bug #62441 Previous Comments: ------------------------------------------------------------------------ [2012-07-02 06:57:17] laruence@php.net 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 ------------------------------------------------------------------------ [2012-06-28 22:08:54] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62441&edit=1

« previous php.bugs (#195599) next »