php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65111 Calling traits directly with static properties/methods
Submitted: 2013-06-24 16:52 UTC Modified: 2013-06-29 18:53 UTC
From: ryan dot brothers at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.5.0 OS: Linux
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: ryan dot brothers at gmail dot com
New email:
PHP Version: OS:

 

 [2013-06-24 16:52 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-24 17:09 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: gron
 [2013-06-25 04:30 UTC] laruence@php.net
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-25 07:19 UTC] gron@php.net
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 15:15 UTC] felipe@php.net
-Status: Assigned +Status: Not a bug -Assigned To: gron +Assigned To:
 [2013-06-25 15:15 UTC] felipe@php.net
Thanks for explaining it.
 [2013-06-29 18:53 UTC] ryan dot brothers at gmail dot com
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 18:01:34 2024 UTC