Jump to content

Display object's methods visibility


NotionCommotion

Recommended Posts

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
)

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.