php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64514 Traits - ask for get_trait_methods
Submitted: 2013-03-25 17:27 UTC Modified: 2013-03-26 13:51 UTC
From: namarpi at yahoo dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.4.13 OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: namarpi at yahoo dot com
New email:
PHP Version: OS:

 

 [2013-03-25 17:27 UTC] namarpi at yahoo dot com
Description:
------------
---
From manual page: http://www.php.net/language.oop5.traits
---

I would like to ask for a method which should be called get_trait_methods, and acts like the get_class_methods.

Test script:
---------------
// file1.php
trait My_Trait {
    function trait_method_1() {}
    function trait_method_2() {}
}

// file2.php
class My_Class {
    use My_Trait;

    function class_method_1() {
        $trait_methods = get_class_methods( 'My_Trait' ); // <-- get_trait_methods
print_r( $trait_methods );
    }

    function class_method_2() {}
}

$my_object = new My_Class();
$my_object->class_method_1();


Expected result:
----------------
Array
(
    [0] => trait_method_1
    [1] => trait_method_2
)

Actual result:
--------------
Recently get_class_methods does this job.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-26 03:59 UTC] laruence@php.net
could you please show us a real usage for this?
 [2013-03-26 03:59 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2013-03-26 05:08 UTC] namarpi at yahoo dot com
It is a WordPress example, and I am already using the New solution:

// Old solution:

class My_Class {

    function method() {
        [...]
        foreach( $things as $index => $thing ) {		
            call_user_func_array(
                array( $this, $thing ),
                array( $arg1, $arg2, $arg3 )
            );
        }
        [...]
    }
}

-------------------------------------------------------------------------------------------
// New solution:

class My_Class {

    function __construct() {
        [...]
        $trait_methods = get_class_methods( 'My_Trait' );
        foreach( $trait_methods as $trait_method ) {
            add_action( "id/$trait_method", array( 'My_Trait', "$trait_method" ), 5, 3 );
        }
        [...]
    }
}
 [2013-03-26 05:08 UTC] namarpi at yahoo dot com
-Status: Feedback +Status: Open
 [2013-03-26 06:53 UTC] namarpi at yahoo dot com
The aim is not to bother My_Class with plenty of methods, I can pick up them, and put them into a separate My_Trait file. And, the other advantage is that the user do not need to dig into the My_Class, it is enough him/her to know the more simpler My_Trait.
 [2013-03-26 08:02 UTC] laruence@php.net
you may want to see: http://php.net/manual/en/function.class-uses.php


and works like: get_class_methods(current(class_uses("My_Class")));
 [2013-03-26 09:04 UTC] namarpi at yahoo dot com
Thank you for the hint, but it leads me to complete the example a little bit.
The problem is the method name conflict and the absence of possibility of resolving
them dynamically (or, I do not know the know-how).

Please have a look at the new example which contains three files:
My_Class, My_Trait_Admin, My_Trait_Front.

https://github.com/NaMarPi/php-reports/blob/master/trait_conflict_resolution.php
 [2013-03-26 09:26 UTC] namarpi at yahoo dot com
Sorry, the problem is that that I do not know which trait will be the dominent
at the place of "use My_Trait_Admin, My_Trait_Front".
 [2013-03-26 13:26 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2013-03-26 13:26 UTC] nikic@php.net
I have a hard time understanding what it is that you want. As you already pointed out, you can use get_class_methods to get the methods of both classes and traits (and interfaces for that matter). Are you suggesting that we add a get_trait_methods alias? If so, no, that won't be done.
 [2013-03-26 13:51 UTC] namarpi at yahoo dot com
Thank you very much for your time. I come with a strong typing background,
and tend to forget about PHP attributes, sorry for that.
And of course, an alias does not make sense.
 [2013-03-26 13:51 UTC] namarpi at yahoo dot com
-Status: Wont fix +Status: Closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jan 05 04:01:29 2025 UTC