Edit report at https://bugs.php.net/bug.php?id=65111&edit=1
ID: 65111
User updated by: ryan dot brothers at gmail dot com
Reported by: ryan dot brothers at gmail dot com
Summary: Calling traits directly with static
properties/methods
Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Linux
PHP Version: 5.5.0
Block user comment: N
Private report: N
New Comment:
Thank you for the feedback. I see that the same functionality exists with
abstract classes. If an abstract class has a static property or method, you
can call it directly. In my particular case, I would like to enforce that all
calls go to the classes that use the traits, and to prevent accidental calls to
the traits directly. I will work around it in a different manner. Thanks.
Previous Comments:
------------------------------------------------------------------------
[2013-06-25 15:15:00] [email protected]
Thanks for explaining it.
------------------------------------------------------------------------
[2013-06-25 07:19:04] [email protected]
Yes, traits are not supposed to be instantiated. However, this behavior is not
really related to instantiation.
Traits are still a lexical entity, i.e., a programming language entity that
provides a lexical scope when defined. Since PHP allows us to define functions
in
such scopes, we can define also static methods on traits.
The way to avoid that would be to change the grammar for traits.
I decided to not do that back in the day. And I still feel that it is unclear
whether it would be conceptually cleaner if it is not possible to defined
static
state/methods.
So, I would leave it as it is. Except, someone comes up with a good reason to
change it. But it would also be quite a bit of a BC issue.
------------------------------------------------------------------------
[2013-06-25 04:30:41] [email protected]
hmm, trait is actually a class in php, I have a quick look into this..
changing
this will needs a significant works..
actually, the is_callable / do_call alreay is a little mess now. I mean the
codes
------------------------------------------------------------------------
[2013-06-24 16:52:52] ryan dot brothers at gmail dot com
Description:
------------
The documentation for traits indicates that "it is not possible to instantiate
a Trait on its own", but I have noticed that a trait can be called directly if
it has a static property or method, as per the below example. Is this intended
behavior? Or should traits be restricted from being called directly? I was
under the impression that traits cannot be called directly per the
documentation. If not, is there a way to prevent traits from being called
directly as in the below example?
Test script:
---------------
<?php
trait t1
{
public static $a1 = 'test1';
public static function test()
{
return 'test2';
}
}
echo t1::$a1."\n";
echo t1::test()."\n";
Expected result:
----------------
Fatal Error that traits cannot be called directly.
Actual result:
--------------
test1
test2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65111&edit=1