Creating PHP Class Instance With a String

Creating PHP class instance with a string

Yes, you can!

$str = 'One';
$class = 'Class'.$str;
$object = new $class();

When using namespaces, supply the fully qualified name:

$class = '\Foo\Bar\MyClass'; 
$instance = new $class();

Other cool stuff you can do in php are:

Variable variables:

$personCount = 123;
$varname = 'personCount';
echo $$varname; // echo's 123

And variable functions & methods.

$func = 'my_function';
$func('param1'); // calls my_function('param1');

$method = 'doStuff';
$object = new MyClass();
$object->$method(); // calls the MyClass->doStuff() method.

Creating PHP class instance with a string into Symfony2

Try this:

foreach ($files as $file) {
$class = 'DIVE\FileUploaderBundle\Form\' . $fileType . 'Type';
// ...
}

Actually you could find the answer in the last comment of the accepted answer to the very question you linked to:

Please note the when using namespaces, you must supply the full path: $className = '\Foo\Bar\MyClass'; $instance = new $className(); – Giel Berkers Dec 16 '14 at 8:23

Basically, in order to instantiate a class from a string, you must use the fully qualified name of the class - including the namespace. Check the page Namespaces and dynamic language features from PHP Manual for a quick explanation and examples.

PHP How to create instance of class returned as string from method invocation

This is not possible on PHP because 'new' needs a string or variable with a string.
The () characters are used for aritmethic associations and for parameters on languaje constructions, and you can't assign to new the result of a call to another function.

Using variable with string for new class initialization

When using string based creation, you must use the fully qualified namespace of the class regardless of the local namespace.

use My\Stuff;
$o = new Stuff();
$name = ‘My\Stuff’;
$o = new $name();
$name = Stuff::class;
$o = new $name();

Create instance of class based on string for name and array for parameters

If you have 5.6:

$instance = new $class(...$args);

If not use reflection:

$reflect  = new ReflectionClass($class);
$instance = $reflect->newInstanceArgs($args);

Can PHP instantiate an object from the name of the class as a string?

Yep, definitely.

$className = 'MyClass';
$object = new $className;

Class not found when creating a class with class name being a string

I believe what's happening is PHP gets confused when you try to instantiate a class whose class name is in a variable and it has to do with imports.

Solution 1

Set your $class variable to the fully qualified class name including the namespace and it should work.

In this way, new $class() should work even while including parenthesis.

Solution 2

After further testing, it seems when you instantiate a variable class, it always assumes global namespace.

With this in mind, you can use class_alias to alias each of your classes. In config/app.php, you can add each class to the aliases array.

'aliases' => [
....

'Example' => App\Example::class
]


Related Topics



Leave a reply



Submit