Solved

object is the same as className

Posted on 2014-04-28
5
170 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 51

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 51

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 51

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

708 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

18 Experts available now in Live!

Get 1:1 Help Now