php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62441 Incorrect strong typing in namespaced child classes
Submitted: 2012-06-28 22:08 UTC Modified: 2012-07-02 06:57 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: eric at wepay dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.3.14 OS: OS X, CentOS
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: eric at wepay dot com
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-02 06:57 UTC] laruence@php.net
-Status: Open +Status: Verified
 [2012-07-02 06:57 UTC] 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
 [2015-08-28 17:00 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ccec609b82b69ef96bc52f7c563c7eb8a68f99a0
Log: Fix bug #62441
 [2015-08-28 17:00 UTC] nikic@php.net
-Status: Verified +Status: Closed
 [2015-09-03 18:10 UTC] ab@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ccec609b82b69ef96bc52f7c563c7eb8a68f99a0
Log: Fix bug #62441
 [2016-07-20 11:36 UTC] davey@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ccec609b82b69ef96bc52f7c563c7eb8a68f99a0
Log: Fix bug #62441
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 12:01:30 2024 UTC