I faced this issues when trying to build a micro framework in PHP (like flask in Python ^^) for our company. So, I've recently dug into PHP core functions. It's make me happier than using framework like Yii but it took more time ^^.
Assuming that we have the following class called CodingTip:
class CodingTip{ public $url; private $postNums; protected $title; public $nullProperty; static $staticProperty; function __construct(){ $this->url = "http://codingtip.blogspot.com"; $this->postNums = 100; $this->title = "just for sharing"; } public function method1($param = 1){ switch ($param){ case 1: return get_object_vars($this); case 2: return $this->method2(); case 3: return $this->method3(); } } protected function method2(){ return get_class_methods(get_class($this)); } private function method3(){ return get_class_vars(get_class($this)); } }Just create new object to test:
//create $ct object outside class $ct = new CodingTip();
To gets the properties of the given object.
I used get_object_vars core function (PHP4 & PHP5)array get_object_vars ( object $object )Using this function outside of class
Returns an associative array of defined object accessible non-static properties for the specified object in scope. If a property has not been assigned a value, it will be returned with a NULL value.
echo "We just got the public properties of object (url and nullProperty), and couldn't get static property, too."; print_r(get_object_vars($ct)); echo "";
Array ( [url] => http://codingtip.blogspot.com [nullProperty] => )By calling method1() without parameter of CodingTip class:
echo "We can get all properties (excepts static one) of current object in all scopes."; print_r($ct->method1()); echo "";
Array ( [url] => http://codingtip.blogspot.com [postNums] => 100 [title] => just for sharing [nullProperty] => )You can also try get_class_vars function, which will returns an associative array of declared properties visible from the current scope, with their default value. For example:
echo "Result: an array of all properties which were declared in class. All attributes have null value in default."; print_r($ct->method1(3)); echo "";
Array ( [url] => [postNums] => [title] => [nullProperty] => [staticProperty] => )
To get all methods of the given Object
In Php, we just use get_class_methods function to get all methods of a class.array get_class_methods ( mixed $class_name )In order to get all methods of the given object, we need get class of that object and use get_class_methods above. For example, Let's get all methods of $ct:
Returns an array of method names defined for the class specified by class_name. In case of an error, it returns NULL.
echo "The result is array of construct and public method1 :"; print_r(get_class_methods(get_class($ct))); echo "";
Array ( [0] => __construct [1] => method1 )In case we call method3 of CodingTip class like the followings:
echo "We will get all methods of current object"; print_r($ct->method1(3)); echo "";
Array ( [0] => __construct [1] => method1 [2] => method2 [3] => method3 )
Reference: