REFLECTION / METAOBJECTS IN PHP


class Foo {
public function helloFoo(){
echo 'hello foo.<br />';
}

public function helloFoo2(){
echo 'hello foo2.<br />';
}
}

class Bar {

public function helloBar(){
echo 'hello Bar.<br />';
}

public function helloBar2(){
echo 'hello Bar2.<br />';
}
}

$reflector = new ReflectionClass('Foo');
$foo = $reflector->newInstance();

$hello = $reflector->getMethod('helloFoo');
$hello->invoke($foo);

$hello2 = $reflector->getMethod('helloFoo2');
$hello2->invoke($foo);

//ANOTHER CALL IN OTHER CLASS REFLECTION

$reflector = new ReflectionClass('Bar');
$bar = $reflector->newInstance();

$helloBar = $reflector->getMethod('helloBar');
$helloBar->invoke($bar);

$helloBar2 = $reflector->getMethod('helloBar2');
$helloBar2->invoke($bar);

//OUTPUT

/*

hello foo.
hello foo2.
hello Bar.
hello Bar2.

*/

This entry was posted in PHP. Bookmark the permalink.

Leave a comment