在php中,可以使用get_class_methods()函數(shù)來獲取對(duì)象的所有方法名,該函數(shù)可獲取指定類(對(duì)象)的所有方法名,并將方法名組成數(shù)組來返回,語法“get_class_methods($obj)”。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
在php中,可以使用get_class_methods()函數(shù)來獲取對(duì)象的所有方法名。
get_class_methods()函數(shù)可獲取指定類(對(duì)象)的所有方法名,并且組成一個(gè)數(shù)組。如果出錯(cuò),則返回 null。
<?php class Website { public $name, $url, $title; // method 1 public function demo1() { return (true); } // method 2 function demo2() { return (true); } // method 3 function demo3() { return (true); } } //實(shí)例化對(duì)象 $student = new Website(); $methods = get_class_methods($student); var_dump($methods);
擴(kuò)展知識(shí):如何獲取當(dāng)前的類名和方法名?
想要獲取當(dāng)前的類名和方法名,可以利用魔術(shù)常量“__CLASS__”、“__FUNCTION__”和“__METHOD__”
-
__CLASS__
:當(dāng)前的類名(包括該類的作用區(qū)域或命名空間);自 PHP 5 起本常量返回該類被定義時(shí)的名字(區(qū)分大小寫)。在 PHP 4 中該值總是小寫字母的。
-
__FUNCTION__
:當(dāng)前函數(shù)(或方法)的名稱; -
__METHOD__
:當(dāng)前的方法名(包括類名);返回該方法被定義時(shí)的名字(區(qū)分大小寫)。
推薦學(xué)習(xí):《PHP視頻教程》