Skip to content Skip to sidebar Skip to footer

Php Passing A Class As A Reference?

in Python, you could do something like this: class SomeClass(object): pass s = SomeClass someClassInstance = s() How could you accomplish the same effect in PHP? From what I under

Solution 1:

You can create instances of dynamic class names; simply pass the name of the class as a string:

classSomeClass{}
$s = 'SomeClass';
$someClassInstance = new$s();

Solution 2:

Using reflection:

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

The nice thing about using reflection is that it throws a catchable exception in the event the class does not exist.

Post a Comment for "Php Passing A Class As A Reference?"