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

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

 New Comment:

Actually, I think it's a improvement of PHP-5.4, this change is introduced by 
fixing this issue: https://bugs.php.net/bug.php?id=60573

before this, you declare 
class A implements IComparable{
        public function equals(self $other){
                return ($this->var == $other->var) ? 'equal' : 'different';
        }
}

actullay the self in here is A, not IComparable. 

but in the IComparable, the self means IComparable itsself.

In scrupulously, A is_a Icomparable, but not equal to Icomperable, what do you 
think? 

thanks


Previous Comments:
------------------------------------------------------------------------
[2012-05-03 16:48:08] larue...@php.net

Oh, sorry I misunderstanded , assign to my self , should have Sth to do with a 
fix 
made by me

------------------------------------------------------------------------
[2012-05-03 15:22:17] jenwelsh at yahoo dot com

The reason I "think" it did work, is because it is **currently** working on a 
production site with PHP 5.3.11.  And it **has** been working for over 2 years.

------------------------------------------------------------------------
[2012-05-03 14:52:08] larue...@php.net

or you can change it to a feature request :)

------------------------------------------------------------------------
[2012-05-03 14:45:32] larue...@php.net

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

------------------------------------------------------------------------
[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

Reply via email to