I got into a conversation with a coworker and we talked about reflection in PHP language. I insisted that instantiating instances of class using variable variables syntax like following is a reflective technique.


$className = 'Animal';
$animal = new $className;

My coworker differed in opinion. I did extensive google search and even read this wikipedia article on reflection. However, to my surprise none of the resources explicitly mentions that this technique is an example of reflection. All I get while doing search is references and examples to Reflection API.

So can someone answer weather this technique is an example of reflection. Also if possible, please point out an authoritative source which explicitly states this.

  • 4
    This doesn't look like reflection to me. Reflection is a means to examine a class to observe its metadata (which includes things like member names). No metadata is being observed here; rather, a string substitution is being performed. In other words, whether you type the name of the class out yourself or provide it as the value of a variable makes no difference to the PHP interpreter. You're looking at a first-class feature of the language itself. Nov 17, 2017 at 16:00
  • Can you please post that as an answer?
    – Juzer Ali
    Nov 19, 2017 at 7:45
  • Unfortunately I don't know for sure that this is how PHP functions. But since PHP is an interpreter, string substitution is all that is really required to make variable variables work; so it seems unlikely that it could be anything else. Nov 20, 2017 at 2:43
  • 1
    Because the Python and Javascript "interpreters" do not perform the kind of string substitution I describe. Absence of evidence is not evidence of absence. Nov 20, 2017 at 15:55
  • 1
    As I stated before, I don't think simple string substitution counts as reflection. You can perform that sort of substitution in an interpreted language quite easily, without spinning up any additional machinery like you have to in languages like Java and C#. Nov 22, 2017 at 15:41

3 Answers 3



Some languages would have to do that via reflection, but in PHP it's a first class feature of the language. It's also not reflection to use eval to do the same thing.

Reflection takes something, an object, type, the name of a function or variable and returns information about it and let's you use the returned information to do stuff (including possibly calling constructors if the thing is a class).

To give an analogy: multiplication can be done via addition, that doesn't make multiplication addition.

In a comment to the OP @RobertHarvey says that he isn't sure it isn't Relection because he isn't sure how the interpreter is creating the new object, but he is missing the point. Regardless of how the interpreter is getting the new class it isn't Reflection because it isn't the APPLICATION getting information about itself and then creating a new class with that information. If the application had gotten a list of class names and picked one at random and created the class that way, that would be Reflection. For a concrete instance it isn't Exec or sp_execSQL that gives TSQL the ability to do Reflection, it is the sys and information_schema views in combination with them.

I would suggest that you read some of the questions asking how to add reflection to languages that don’t support it: https://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application, https://stackoverflow.com/questions/1353022/reflection-support-in-c/1353133#1353133 and https://stackoverflow.com/questions/1118705/call-a-function-named-in-a-string-variable-in-c. In particular note how the fundamental problem is that either the language doesn’t supply access to the required information or that the construct might not exist at runtime.

  • Not sure if I agree with a general answer of "no" yet. But, I'm also unconvinced that reflection necessarily deals in objects. Nor am I convinced that reflection must be implemented in such completeness to be considered reflection...
    – svidgen
    Jan 22, 2018 at 16:06
  • @svidgen: why call it reflection if doesn't do what reflection does? Would you call a user defined factory class that took either a string or a type and returned a new instance of the class reflection? I certainly wouldn't, any more than I would call a dictionary of functions that returned a new instance of a type reflection. The details of how it operates and what needs to be done to get it to do it are important.
    – jmoreno
    Jan 23, 2018 at 1:19
  • Well, I wouldn't. But, reflection is much simpler than your answer infers. It's nothing more than a program being able to "reflect" upon itself. (The power to examine and manipulate itself). The means by which this occurs do not diminish reflectiveness. And while a more limited reflective ability is less reflective, it's not not reflection. ... Your answer seems to limit reflection to the manipulation of instantiable objects. And, that would incorrectly imply that non-oo languages (and non-oo areas of on langs) cannot reflect.
    – svidgen
    Jan 23, 2018 at 1:32
  • @svidgen: I certainly didn't limit it to instantiable objects, object types includes interfaces and static classes. Reflection means that there is a method of getting information about the code at runtime. Javascripts for(property in Object) would qualify. Vbscripts GetRef("name") or Eval wouldn't. The example given by the OP doesn't allow examining.
    – jmoreno
    Jan 23, 2018 at 10:33
  • True. Your answer refers to "object types." But, it's a minor difference. Point is, you're defining reflection in OOP terms, and it's not simply an OO ability.
    – svidgen
    Jan 23, 2018 at 15:49

I think the key is in the modify its own structure and behavior at runtime part.

When you write a program in a more "static" style, you know all your instantiated classes in advance. Take any creational patterns. All the created classes are known in advance. Their names already present in code (and I assume that in your example the class name is calculated in runtime). And if there is no such class, any decent language won't compile. And your colleague relies on a string value that is not known in advance and defined at runtime. Here is a tricky part. From one side, any if clause can be considered as modification of behavior at runtime. But it's wrong. This behavior is already present in source code. Any condition results in already defined behavior, be it an instantiated class name or called method. But when you define your class name in advance, you never know for 100% sure whether it always results in a valid value suitable for a class name, for example. It's a bit difficult to comprehend and agree with it if your only language is PHP, which is dynamically typed by nature, so probably it doesn't look that weird. But if you'd look at this problem from a statically-typed language perspective, it'd make a perfect sense.

I'm still not absolutely sure about my arguments, but I tried to figure it out, since I agree with you and want to prove it as well.

  • So... you're saying this is not reflection? Nov 17, 2017 at 15:49
  • I'm saying quite the opposite. Nov 17, 2017 at 16:15

The wikipedia article about Reflection (Computer Programming) now explicitly states that the usage in question is an example of reflection.

Moreover in the Implementations section the article clearly describes reflective feature as follows:

Some of these features are the abilities to... Convert a string matching the symbolic name of a class or function into a reference to or invocation of that class or function.

So as far as I can tell at the moment, this is an example of reflection.

  • 5
    So we'll just take your word for it, I guess? Nov 17, 2017 at 15:49
  • Your sarcasm is misguided. I am here to find this out. The Wikipedia article was ambiguous, I made an edit and it was accepted. This made me believe that that was the case. You can contribute an answer if you have one and I'll gladly accept it if it answers my question.
    – Juzer Ali
    Nov 19, 2017 at 7:48
  • 4
    Making an edit to Wikipedia and waiting a few days to see if it gets reverted is not proof. Anyone can make an edit to Wikipedia; edits are not "accepted" in that sense. Nov 20, 2017 at 2:41
  • 3
    Did anyone else's confidence in wikipedia just go down a touch?
    – svidgen
    Jan 22, 2018 at 16:25
  • 1
    Wikipedia is not a source of authority here. Especially since you are basically citing yourself.
    – Michael
    Sep 23, 2018 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.