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.

*/

Advertisements
This entry was posted in PHP. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s