NotionCommotion Posted March 29, 2017 Share Posted March 29, 2017 (edited) get_class_methods() does a good job of outputting the methods of a given object or class. How can I also show the method's visibility? PS. My purpose is just for troubleshooting. EDIT. Also, how about also including methods defined in the object's parent? Edited March 29, 2017 by NotionCommotion Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 29, 2017 Share Posted March 29, 2017 Reflection Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 29, 2017 Author Share Posted March 29, 2017 Actually, it does display the parent's methods... <?php class class1 { public function public11(){} public function public2(){} public function protected11(){} public function protected2(){} public function private11(){} public function private2(){} } class class2 extends class1 { public function public21(){} public function public2(){} public function protected21(){} public function protected2(){} public function private21(){} public function private2(){} } class class3 extends class2 { public function public31(){} public function public2(){} public function protected31(){} public function protected2(){} public function private31(){} public function private2(){} } $o=new class3; print_r(get_class_methods($o)); Array ( [0] => public31 [1] => public2 [2] => protected31 [3] => protected2 [4] => private31 [5] => private2 [6] => public21 [7] => protected21 [8] => private21 [9] => public11 [10] => protected11 [11] => private11 ) Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 29, 2017 Author Share Posted March 29, 2017 Reflection Thanks Jacques1. I suspected so, so I had previously looked at this example at http://php.net/manual/en/reflection.examples.php, but it was only a shell command, so I wasn't sure. I am sure I can figure it out, and appreciate the confirmation. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted March 29, 2017 Author Share Posted March 29, 2017 Typo on my earlier example. Looks like the only way to view the methods is within the object. <?php abstract class class1 { public function public11(){} public function public2(){} protected function protected11(){} protected function protected2(){} private function private11(){} private function private2(){} } abstract class class2 extends class1 { public function public21(){} public function public2(){} protected function protected21(){} protected function protected2(){} private function private21(){} private function private2(){} } class class3 extends class2 { public function __construct() { print_r(get_class_methods($this)); } public function public31(){} public function public2(){} protected function protected31(){} protected function protected2(){} private function private31(){} private function private2(){} } $o=new class3; print_r(get_class_methods($o)); Array ( [0] => __construct [1] => public31 [2] => public2 [3] => protected31 [4] => protected2 [5] => private31 [6] => private2 [7] => public21 [8] => protected21 [9] => public11 [10] => protected11 ) Array ( [0] => __construct [1] => public31 [2] => public2 [3] => public21 [4] => public11 ) Quote Link to comment Share on other sites More sharing options...
requinix Posted March 29, 2017 Share Posted March 29, 2017 get_class_methods() is subject to scope, just like get_object_vars() and the other similar functions. If you want the complete list of methods then you must use reflection. Don't use it for normal code, though: reflection is expensive and helps you create weird bugs. There are better options. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.