Bug #62441 [Opn->Ver]: Incorrect strong typing in namespaced child classes
Edit report at https://bugs.php.net/bug.php?id=62441&edit=1
ID: 62441
Updated by: laruence@php.net
Reported by: eric at wepay dot com
Summary: Incorrect strong typing in namespaced child classes
-Status: Open
+Status: Verified
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:
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
Previous Comments:
------------------------------------------------------------------------
[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
Thread (3 messages)