Solved

object is the same as className

Posted on 2014-04-28
5
171 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 52

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 52

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 52

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now