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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: motammem at gmail dot com
New email:
PHP Version: OS:

 

 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 22:01:28 2024 UTC