php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60573 type hinting with "self" keyword causes weird errors
Submitted: 2011-12-20 10:56 UTC Modified: 2012-03-02 03:32 UTC
Votes:3
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: jpauli@php.net Assigned: laruence
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.8 OS: *nix
Private report: No CVE-ID:
 [2011-12-20 10:56 UTC] jpauli@php.net
Description:
------------
"self" should be thought as "the class where the keyword is written in".

Here is then a strange behavior using inheritance :

Test script:
---------------
class Foo {

public function setSelf(self $s) { }

}

class Bar extends Foo {

public function setSelf(self $s) { }

}


Expected result:
----------------
Strict Standards as Bar::setself() signature doesn't respect Foo::setSelf() 
signature.
Foo's one has a type hint on Foo (using self keyword), but Bar's one on Bar 
(still using self keyword) : the methods then have a signature mismatch, and PHP 
should complain about that

Actual result:
--------------
Nothing happens.

Trying this gives the correct E_STRICT error, correct behavior :

class Foo {

public function setSelf(Foo $s) { }

}

class Bar extends Foo {

public function setSelf(Bar $s) { }

}


Patches

bug60573.phpt (last revision 2011-12-20 16:01 UTC) by laruence@php.net)
bug60573.patch (last revision 2011-12-20 16:01 UTC) by laruence@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-20 12:31 UTC] laruence@php.net
This is really a tough one,  think about following:

<?php
class Foo {

public function setSelf(self $s) { }

}

class Bar extends Foo {

public function setSelf(parent $s) { }

}
?>
---------------------
<?php
class Foo {

public function setSelf(Foo $s) { }

}

class Bar extends Foo {

public function setSelf(parent $s) { }

}
-----------------------
<?php
class Base {
}

class Foo extends Base{

public function setSelf(parent $s) { }

}

class Bar extends Foo {

public function setSelf(Base $s) { }

}


and more.......
 [2011-12-20 12:52 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2011-12-20 12:52 UTC] laruence@php.net
-Status: Assigned +Status: Open
 [2011-12-20 12:53 UTC] laruence@php.net
I have a patch now, will apply it after do careful testings
 [2011-12-20 16:01 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug60573.patch
Revision:   1324396875
URL:        https://bugs.php.net/patch-display.php?bug=60573&patch=bug60573.patch&revision=1324396875
 [2011-12-20 16:01 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug60573.phpt
Revision:   1324396902
URL:        https://bugs.php.net/patch-display.php?bug=60573&patch=bug60573.phpt&revision=1324396902
 [2011-12-20 16:04 UTC] laruence@php.net
a patch for php 5.4 was submitted, since 5.4 will generate detailed info about the 
prototype argument list, so this defect is more significant for 5.4.

and Tyrael suggest commit this only after 5.4.0 release. so leave this open, I 
will commit later.

thanks
 [2012-03-02 03:32 UTC] laruence@php.net
Automatic comment from SVN on behalf of laruence
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=323773
Log: MFH: Fixed bug #60573 (type hinting with &quot;self&quot; keyword causes weird errors)
 [2012-03-02 03:32 UTC] laruence@php.net
-Status: Assigned +Status: Closed
 [2012-03-02 03:32 UTC] laruence@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2012-04-18 09:45 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=0b7f94545e95813b3d6392d92deead5b8bd022b8
Log: MFH: Fixed bug #60573 (type hinting with &quot;self&quot; keyword causes weird errors)
 [2012-04-18 09:46 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=d74a258f240e3d1dcb14ec086ba6fe48a4864dad
Log: Fixed Bug #60573 (type hinting with &quot;self&quot; keyword causes weird errors)
 [2012-07-24 23:37 UTC] rasmus@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=0b7f94545e95813b3d6392d92deead5b8bd022b8
Log: MFH: Fixed bug #60573 (type hinting with &quot;self&quot; keyword causes weird errors)
 [2012-07-24 23:37 UTC] rasmus@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=d74a258f240e3d1dcb14ec086ba6fe48a4864dad
Log: Fixed Bug #60573 (type hinting with &quot;self&quot; keyword causes weird errors)
 [2013-09-17 07:19 UTC] andries at centim dot be
This patch seems to cause strange behaviour for interfaces. If the "self" keyword is defined in an interface, I would think that "self" would refer to the implementing class.

Test script which worked in 5.3.19:

<?php

interface Comparable
{
    function compare(self $compare);
}

// Which is then implemented:

class Foo implements Comparable
{
    function compare(self $compare)
    {}
}

class Bar implements Comparable
{
    function compare(self $compare)
    {}
}

$foo = new Foo();
$bar = new Bar();

$foo->compare($foo);
$bar->compare($bar);
 [2013-11-17 09:33 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=0b7f94545e95813b3d6392d92deead5b8bd022b8
Log: MFH: Fixed bug #60573 (type hinting with &quot;self&quot; keyword causes weird errors)
 [2013-11-17 09:34 UTC] laruence@php.net
Automatic comment on behalf of laruence
Revision: http://git.php.net/?p=php-src.git;a=commit;h=d74a258f240e3d1dcb14ec086ba6fe48a4864dad
Log: Fixed Bug #60573 (type hinting with &quot;self&quot; keyword causes weird errors)
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 14:02:33 2014 UTC