Solved

object is the same as className

Posted on 2014-04-28
5
173 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
  • 3
  • 2
5 Comments
 
LVL 55

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 55

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 55

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
How to fix Datetime in MySQL? 4 48
Read a remote csv file from a https URL 8 47
ajax to record click 3 16
Link failure 16 32
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.

809 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