Does PHP pass the class as a reference?

in Python, you can do something like this:

class SomeClass(object): pass
s = SomeClass
someClassInstance = s()

How could you achieve the same effect in PHP? As far as I understand, you can’t do this? It's true?

+2
source share
2 answers

You can instantiate dynamic class names; just pass the class name as a string:

class SomeClass {}
$s = 'SomeClass';
$someClassInstance = new $s();
+7
source

Using reflection:

class SomeClass {}
$s = new ReflectionClass('SomeClass');
$someClassInstance = $s->newInstance();

The best part about using reflection is that it throws a spectacular exception if the class does not exist.

+1
source

All Articles