|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 09:00:01 2025 UTC |
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 ); } [...] } }