Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

object is the same as className

<?php
class HelloWorld{
  public function sayHelloTo($name){
    return 'Hello '.$name;
  }
}

$obj=new HelloWorld();
//$reflectionMethod=new ReflectionMethod('HelloWorld','sayHelloTo');
$reflectionMethod=new ReflectionMethod($obj,'sayHelloTo');
echo $reflectionMethod->invoke(new HelloWorld(),'Mike');

Open in new window


please explain why
//$reflectionMethod=new ReflectionMethod('HelloWorld','sayHelloTo');
$reflectionMethod=new ReflectionMethod($obj,'sayHelloTo');

are the same
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

The answer is in the documentation for ReflectionMethod
http://www.php.net/manual/en/reflectionmethod.construct.php
 class
    Classname or object (instance of the class) that contains the method.

Open in new window

In other words the constructor for the Reflection method can take either

- The name of a class
OR
- An already instantiated (object) instance of a class
The commented out line above does the former - it passes the class name in as a string. The ReflectionMethod constructor recognises it is a string and will automatically create an object of that class type.
If it is an object (as in the second line) then it will simply use that object.
Avatar of rgb192

ASKER

Ok so in all the examples of php: is 'className' same as $obj?
Or is the reflection documentation saying it will take either
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

Thanks for example of what is in reflection class
You are welcome - thanks for the points.