PHP Namespace with Dynamic Class Name

PHP namespace with Dynamic class name

Well, just spell out the namespace in the string:

$definition = Definer::defineProfile($_SESSION['user']->UserType);
$class = '\\Editor\\' . $definition;
$foo = new $class();

And if it's a child namespace (as indicated in the comments), simply prepend the namespace with __NAMESPACE__:

$class = __NAMESPACE__ . '\\Editor\\' . $definition;

So if the current namespace is \Foo\Bar, and $definition is "Baz", the resulting class would be \Foo\Bar\Editor\Baz

PHP dynamic namespaces

Not possible. Namespaces, imports and aliases are resolved at compile time.

However, it is possible to create objects from a class name that is built at runtime:

$className = "common\\components\\cfoBi\\i18n\\{$countryCode}\\gimmea";

$object = new $className();

See PHP docs: http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new

PHP Dynamic Class Loading w/ Namespaces

The following line:

$this->controller = new Controllers\$this->controller;

is not valid PHP code.

If you need to instantiate classes using a dynamic class name, use an intermediate variable:

$className = 'Controllers\\' . $this->controller;
$this->controller = new $className();

PHP namespace and dynamic classname

The "problem" here is actually at a lower level than SPL, and can also be seen with __autoload(). This is best demonstrated in code:

function __autoload ($class) {
echo "Loading $class\n";
}

new Test;
// displays "Loading Test"

$var = 'Test';
new $var;
// displays "Loading Test"

// However, when we introduce namespaces...

new \This\Is\A\Test;
// displays "Loading This\Is\A\Test"

$var = '\This\Is\A\Test';
new $var;
// displays "Loading \This\Is\A\Test"

Notice how when when introduce namespaces, the leading slash is not passed to the function when called statically, but it is when called dynamically.

The solution is therefore to do either of these fairly simple things:

  • Remove the leading slash from the dynamic class name instantiation.
  • Wrap the default spl_autoload() function like this:
set_include_path(get_include_path().PATH_SEPARATOR."classes".PATH_SEPARATOR."utils");
spl_autoload_extensions(".class.php");
spl_autoload_register(function($class) {
spl_autoload(ltrim($class, '\\'));
});

Of course if you are doing this, you could also remove the call to spl_autoload_extensions() and just pass the ".class.php" string to the second argument of spl_autoload()

dynamic class names in php

This should work to instantiate a class with a string variable value:

$type = 'Checkbox'; 
$field = new $type();
echo get_class($field); // Output: Checkbox

So your code should work I'd imagine. What is your question again?

If you want to make a class that includes all extended classes then that is not possible. That's not how classes work in PHP.

Using namespaces with classes created from a variable

Because strings can be passed around from one namespace to another. That makes name resolution ambiguous at best and easily introduces weird problems.

namespace Foo;

$class = 'Baz';

namespace Bar;

new $class; // what class will be instantiated?

A literal in a certain namespace does not have this problem:

namespace Foo;

new Baz; // can't be moved, it's unequivocally \Foo\Baz

Therefore, all "string class names" are always absolute and need to be written as FQN:

$class = 'Foo\Baz';

(Note: no leading \.)

You can use this as shorthand, sort of equivalent to a self-referential self in classes:

$class = __NAMESPACE__ . '\Baz';

Dynamic namespaced class with alias

Only the parser uses your namespace aliases to canonicalize the class references inside each of your files.

In other words, it doesn't introduce some kind of global alias that other code can use. Once your script has been parsed, the alias is no longer used.

This behaviour is also described in the manual:

Importing is performed at compile-time, and so does not affect dynamic class, function or constant names.



Related Topics



Leave a reply



Submit