Solved

object is the same as className

Posted on 2014-04-28
5
175 Views
Last Modified: 2014-04-29
<?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
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 40028894
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.
0
 

Author Comment

by:rgb192
ID: 40029764
Ok so in all the examples of php: is 'className' same as $obj?
Or is the reflection documentation saying it will take either
0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40029851
Ok so in all the examples of php: is 'className' same as $obj?
No definitely not
What the code is doing is probably something like this
public ReflectionMethod::__construct ( mixed $class , string $name )
{
    // CHECK IF $class IS AN OBJECT
    if (!is_object($class)) {
       // IF NOT CHECK IF A CLASS EXISTS WITH NAME $class
       if (class_exists($class)) {
          // IF SO INSTANTIATE IT
          $obj = new {$class};
       }
       else {
          //THROW AN ERROR HERE - class not found
       }
    }
    // IF $class IS AN OBJECT THEN JUST ASSIGN IT
    else {
       $obj = $class;
    }
}

Open in new window

The above is just an illustration - I cannot confirm if the actual code follows this pattern. The point being that in PHP there is a distinction between a class name and an instance of the class - you cannot use them interchangeably.
0
 

Author Closing Comment

by:rgb192
ID: 40029896
Thanks for example of what is in reflection class
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 40029946
You are welcome - thanks for the points.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question