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

 ID:                 55268
 Updated by:         ras...@php.net
 Reported by:        php-bugs at majkl578 dot cz
 Summary:            Traits cannot be used as function/method typehints
 Status:             Bogus
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Irrelevant
 PHP Version:        5.4SVN-2011-07-22 (snap)
 Block user comment: N
 Private report:     N

 New Comment:

But traits and interfaces are completely different. An interface is a contract 
a 
class has to meet and carries no implementation, while a trait is purely 
implementation. If you want to specify that a class has to have the 
implementation 
that a trait provides write an interface that describes the trait.


Previous Comments:
------------------------------------------------------------------------
[2011-07-22 15:05:28] php-bugs at majkl578 dot cz

Maybe the example was too brief.

1. It was intended to work the same way as interfaces do: function 
foo(SomeInterface $foo) where $foo must be any class implementing SomeInterface 
or any of its subclass.

2. Obviously, it was empty just to make the code short.

3. I know it's just a code snippet, but it'd be still useful for cases 
mentioned above.

------------------------------------------------------------------------
[2011-07-22 14:36:10] ras...@php.net

Three things I don't understand here.

1. $obj is clearly an instance of Bar, even if the type-hint worked, it 
wouldn't 
be of type Foo

2. An empty trait makes no sense. This is a NOP.

3. A trait is not a type in any way. It is just a code snippet.

------------------------------------------------------------------------
[2011-07-22 14:22:27] php-bugs at majkl578 dot cz

Description:
------------
It is not possible to use trait as a typehint in function declaration.

Use case: Extending few different classes to use a trait in them where these 
classes doesn't have same parent. Then I want to specify typehint, because I 
require some methods which are declared by that trait.

Test script:
---------------
trait Foo
{}

class Bar {
        use Foo;
}

function test(Foo $obj) {
        echo 'hello';
}

test(new Bar);

Expected result:
----------------
hello

Actual result:
--------------
Catchable fatal error: Argument 1 passed to test() must be an instance of Foo, 
instance of Bar given...


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



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

Reply via email to