get_class_methods() - php 类对象函数
get_class_methods()
(PHP 4, PHP 5, PHP 7)
返回由类的方法名组成的数组
说明
get_class_methods(mixed $class_name): array返回由类的方法名组成的数组。
参数
$class_name类名或者对象实例。
返回值
返回由$class_name指定的类中定义的方法名所组成的数组。如果出错,则返回NULL
。
范例
Example #1get_class_methods()示例
以上例程会输出:
myclass myfunc1 myfunc2
参见
get_class()
返回对象的类名get_class_vars()
返回由类的默认属性组成的数组get_object_vars()
返回由对象属性组成的关联数组
It should be noted that the returned methods are dependant on the current scope. See this example: Output: $this: array 0 => string 'privateMethod' (length=13) 1 => string 'publicMethod' (length=12) 2 => string '__construct' (length=11) C (inside class): array 0 => string 'privateMethod' (length=13) 1 => string 'publicMethod' (length=12) 2 => string '__construct' (length=11) $c: array 0 => string 'publicMethod' (length=12) 1 => string '__construct' (length=11) C (outside class): array 0 => string 'publicMethod' (length=12) 1 => string '__construct' (length=11)
It is important to note that get_class_methods($class) returns not only methods defined by $class but also the inherited methods. There does not appear to be any PHP function to determine which methods are inherited and which are defined explicitly by a class.
I have created a very simple test runner using this function function get_bar($text) { $bar = ""; for($i=1; $irun_tests(); } // run the tests function run_tests() { print("Tester by Minhajul Anwar \n"); $class = get_class($this); $test_methods = preg_grep('/^test/', get_class_methods($this)); foreach($test_methods as $method) { $start_rep = "test: $class::$method"; $bar = get_bar($start_rep); print("\n$start_rep\n$bar\n"); $this->$method(); print("\n"); } } } now you just need to write your test class with tegst methods prefixed by 'test', and then just instantiate object of that test class of your, all those tests methods will get run automatically The drawback is: your test methods must not accept any arguments an example: require '../autoload.php'; register_autoload_paths(realpath('./')); class Test_Test extends Tester { function test_something() { print("method got executed"); } function testAnotherThing() { print("another test method"); } } $Test = new Test_Test();
This function will return only the methods for the object you indicate. It will strip out the inherited methods. function get_this_class_methods($class){ $array1 = get_class_methods($class); if($parent_class = get_parent_class($class)){ $array2 = get_class_methods($parent_class); $array3 = array_diff($array1, $array2); }else{ $array3 = $array1; } return($array3); }
Win32 only: It's probably worth noting here that you can't get the methods of an object created by the built-in 'COM' class. ie - this won't work: $word = new COM('Word.Application'); $methods = get_class_methods(get_class($word)); print_r($methods); Matt
In PHP4, this function converts its return values to lowercase; but in PHP5, it leaves the return values in their original case. This can cause serious problems when trying to write code that dynamically calls a class method, and that works in both PHP4 and PHP5. This code snippet shows one way of achieving compatibility with both versions:
If you use "get_class_methods" to check if a Method is in a Class remember that the function return lowered name of class methods: class classPippo { function DummyFunct() { // Do nothing... } } $aClassMethods = get_class_methods(classPippo); $sMethodName = 'DummyFunct'; // This not work... if (in_array($sMethodName, $aClassMethods)) classPippo::DummyFunct(); // This work... if (in_array(strtolower($sMethodName), $aClassMethods)) classPippo::DummyFunct();
As an extension to onesimus's code below for finding inherited methods, in PHP 5 you can use the Reflection API to find which of these are overriden. e.g.
In PHP4, if you need to get_class_methods in their original case. You can use this simple function I created. // Note: this function assumes that you only have 1 class in 1 file $file = "path/to/myclass.php" function file_get_class_methods ($file) { $arr = file($file); foreach ($arr as $line) { if (ereg ('function ([_A-Za-z0-9]+)', $line, $regs)) $arr_methods[] = $regs[1]; } return $arr_methods; }
Note that this function will answer both class AND instance methods ("class methods" are called "static" in PHP). Sort of a little "trap" for people who have in-depth experience with the OO terminology :-)
If you only need the methods declared by the class, and not the parent classes ones, you can do something like: $declared_methods = array_diff(get_class_methods($class), get_class_methods(get_parent_class($class)));
!Concerning PHP5 Only! If you want to get all methods/functions from a class you can do this with the get_class_methods function. However the drawback on this function in PHP5 is that you will NOT receive protected and private methods of a class/object if you are calling the method from another class/object context. If you want to receive all methods of a given Class in another Class you should use the PHP5 Reflection API. The following source allows to retrieve all methods from a derived class in its (abstract) Base Class. In the example you need to call the base constructor from the derived classes constructor in order to let the base class know the name of the derived class. Use the "__CLASS__" definition for passing the classname of current class to its base class.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)