To do this, you may need to use the PHP Reflection API . However, I should also ask you why you want to do this, because Reflection is usually only used in situations that are a bit hacked to begin with. Perhaps, however, like this:
<?php class Foo { protected $reflection; protected function bar( ) { } private function baz( ) { } public function __call( $method, $args ) { if( ( $reflMethod = $this->method( $method ) ) !== false ) { if( $reflMethod->isPrivate( ) ) { echo "That private.<br />\n"; } elseif( $reflMethod->isProtected( ) ) { echo "That protected.<br />\n"; } } } protected function method( $name ) { if( !isset( $this->methods[$name] ) ) { if( $this->reflect( )->hasMethod( $name ) ) { $this->methods[$name] = $this->reflect( )->getMethod( $name ); } else { $this->methods[$name] = false; } } return $this->methods[$name]; } protected function reflect( ) { if( !isset( $this->reflection ) ) { $this->reflection = new ReflectionClass( $this ); } return $this->reflection; } } $foo = new Foo( ); $foo->baz( ); $foo->bar( );
source share