Jump to content

Recommended Posts

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 by NotionCommotion
Link to comment
https://forums.phpfreaks.com/topic/303570-display-objects-methods-visibility/
Share on other sites

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
)

 

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.

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
)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.