[RESOLVED] Reflection how to call method randomly ...

admin

Administrator
Staff member
Hi.
It's my first time with reflection and I'm not
very confy with it.
My goal is call a method randomly
from an other class.

<?php
error_reporting(E_ALL | E_STRICT);
class A{
public function __construct(){}
public function one(){
echo "I'm One<br />";
}
public function two(){
echo "I'm Two<br />";
}
public function three(){
echo "I'm Three<br />";
}
}
class B{
public function __construct(){}
public function testReflection(){
$a= new A();
$ref= new ReflectionClass(get_class($a));
$methods= $ref->getMethods();
array_shift($methods);
shuffle($methods);
call_user_func(array($a, $methods[0]->name));
}
}
$b= new B();
$b->testReflection();
?>


I'm wondering is it the right way ? :oUsing get_class_methods might be more efficient, since you would not need to create an instance of the Reflection class. At the very least, you might consider having class B extend RefelectionClass, removing the need to instantiate it on each call to the B::testReflection() method.Using get_class_methods might be more efficient, since you would not need to create an instance of the Reflection class. At the very least, you might consider having class B extend RefelectionClass, removing the need to instantiate it on each call to the B::testReflection() method.


class B{
public function __construct(){}
public function testReflection(){
$a= new A();
$methods= get_class_methods($a);
array_shift($methods);
shuffle($methods);
call_user_func(array($a, $methods[0]));
}
}




Thanks a lot buddy :D
 
Back
Top