php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #67273 Traits behave like interfaces
Submitted: 2014-05-14 01:29 UTC Modified: 2014-05-14 13:25 UTC
From: motammem at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.4.28 OS:
Private report: No CVE-ID: None
 [2014-05-14 01:29 UTC] motammem at gmail dot com
Description:
------------
When we use a Trait in a class we define behavior for that class like interfaces.
But we can't use them as an input type. Checkout "Test Script" to find out more.

I think you got idea.
Best regards, motammem

Test script:
---------------
trait Foo {
  sayHello(){
    echo "Hello";
  }
}

class Bar {
  use Foo;
}

function test(Foo $foo){
  $foo->sayHello();
}

//////////////////////////////

$bar = new Bar();

test($bar);

Expected result:
----------------
Hello

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-14 13:25 UTC] mike@php.net
-Status: Open +Status: Not a bug
 [2014-05-14 13:25 UTC] mike@php.net
They do *not* work in any way like interfaces.
Traits are just automated copy&paste.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Aug 09 18:34:40 2020 UTC