Bug #61924 [Opn]: cannot use self in interface function declaration

From: Date: Thu, 03 May 2012 14:45:32 +0000
Subject: Bug #61924 [Opn]: cannot use self in interface function declaration
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-170123@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=61924&edit=1

 ID:                 61924
 Updated by:         laruence@php.net
 Reported by:        jenwelsh at yahoo dot com
 Summary:            cannot use self in interface function declaration
 Status:             Open
 Type:               Bug
 Package:            Class/Object related
 PHP Version:        5.4.1
 Block user comment: N
 Private report:     N

 New Comment:

it's always not allowed since php 5.2,  why are you thinking it worked once?


Previous Comments:
------------------------------------------------------------------------
[2012-05-03 14:20:54] jenwelsh at yahoo dot com

Description:
------------
I am reviewing existing code on a PHP5.4.1 testbed. I've discovered that interface declarations
using 'self' as a type hint no longer allow implementations to use 'self' but
require them to use the interface name.

It is no longer possible for an interface to declare a method that requires the implementor's
class as a typehint without declaring that class specifically. And that would limit the usefulness
of that interface to one class only.

Test script:
---------------
interface IComparable {
	public function equals(self $other);
}

class A implements IComparable{
	protected $var;
	
	public function __construct(self $v){
		$this->var=$v;
	}
	
	public function equals($other){
		return ($this->var == $other->var) ? 'equal' : 'different';
	}
}

$a1= new A(7);
$a2= new A(5);
$a3= new A(5);

echo $a1->equals($a2),"\n";
echo $a2->equals($a3),"\n";

Expected result:
----------------
different
equal


Actual result:
--------------
PHP Fatal error:  Declaration of A::equals() must be compatible with IComparable::equals(IComparable
$other) 


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=61924&edit=1


Thread (10 messages)

« previous php.bugs (#170123) next »