Req #55266 [Com]: Traits vs Type hints
| From: | me at mikestowe dot com | Date: | Fri, 22 Jul 2011 17:36:59 +0000 |
| Subject: | Req #55266 [Com]: Traits vs Type hints | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-162108@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55266&edit=1
ID: 55266
Comment by: me at mikestowe dot com
Reported by: mchlpl at gmail dot com
Summary: Traits vs Type hints
Status: Open
Type: Feature/Change Request
Package: Class/Object related
PHP Version: 5.4.0alpha2
Block user comment: N
Private report: N
New Comment:
I believe this is the correct result as $a is not an instance of the trait but rather of the class
that utilizes the trait.
Previous Comments:
------------------------------------------------------------------------
[2011-07-22 07:44:34] mchlpl at gmail dot com
Description:
------------
Traits, when used in a class, are not added to class' type, and therefore can not be use in
type hints. The RFCs on traits https://wiki.php.net/rfc/traits and on horizontal reuse
https://wiki.php.net/rfc/horizontalreuse do
not explain if this is by design.
On the other hand Traits seem to share the same namespace as Classes and Interfaces, since it is
impossible to have an Interface and a Trait that share the name ('Fatal error: Cannot redeclare
class' is raised when this is attempted).
Test script:
---------------
<?php
trait SomeTrait {}
class SomeClass {
use SomeTrait;
}
$a = new SomeClass();
var_dump(is_a($a,'SomeTrait'));
Expected result:
----------------
bool(true)
Actual result:
--------------
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55266&edit=1