Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating class by string using PHP 5.3 namespaces

I can't get around an issue instantiating a new class by using a string variable and PHP 5.3. namespaces. For example, this works;

$class = 'Reflection';
$object = new $class();

However, this does not;

$class = '\Application\Log\MyClass';
$object = new $class();

A fatal error gets thrown stating the class cannot be found. However it obviously can be instantiated if using the FQN i.e.;

$object = new \Application\Log\MyClass;

I've found this to be aparrent on PHP 5.3.2-1 but not not in later versions. Is there a work around for this?

like image 490
Kevin Avatar asked Feb 21 '11 23:02

Kevin


People also ask

How can I instantiate a class using namespace in PHP?

$object = new Foo_Bar(); It gets a little trickier if your class is defined in a namespace and you're instantiating it from outside of that namespace. If you're hard-coding a class, you need to use a leading backslash to reference the class.

How do you namespace in PHP?

The first thing to do is to define a namespace with the namespace keyword at the top of the PHP file. All the code underneath the namespace keyword becomes namespaced code. It's important to note that this keyword must be placed at the top of the file, making sure that nothing precedes it.

What is class namespace PHP?

Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.

How does PHP namespace work?

Like C++, PHP Namespaces are the way of encapsulating items so that same names can be reused without name conflicts. It can be seen as an abstract concept in many places. It allows redeclaring the same functions/classes/interfaces/constant functions in the separate namespace without getting the fatal error.


2 Answers

$class = 'Application\Log\MyClass'; $object = new $class(); 

The starting \ introduces a (fully qualified) namespaced identifier, but it's not part of the class name itself.

like image 151
Artefacto Avatar answered Sep 22 '22 14:09

Artefacto


Another way to achieve the same result but with dynamic arguments is as follows. Please consider the class below as the class you want to instantiate.

<?php

// test.php

namespace Acme\Bundle\MyBundle;

class Test {
    public function __construct($arg1, $arg2) {
        var_dump(
            $arg1,
            $arg2
        );
    }
}

And then:

<?php

require_once('test.php');

(new ReflectionClass('Acme\Bundle\MyBundle\Test'))->newInstanceArgs(['one', 'two']);

If you are not using a recent version of PHP, please use the following code that replaces the last line of the example above:

$r = new ReflectionClass('Acme\Bundle\MyBundle\Test');
$r->newInstanceArgs(array('one', 'two'));

The code will produce the following output:

string(3) "one"
string(3) "two"
like image 37
Francesco Casula Avatar answered Sep 22 '22 14:09

Francesco Casula