Can PHP Namespaces Contain Variables

Can PHP namespaces contain variables?

No. You can set a variable after declaring a namespace, but variables will always exist in the global scope. They are never bound to namespaces. You can deduce that from the absence of any name resolution descriptions in

  • FAQ: things you need to know about namespaces (PHP 5 >= 5.3.0)

There would also be no allowed syntax to locate variables in a namespace.

print \namespace\$var;      // syntax error

print "${namespace\\var}"; // "unexpected T_NS_SEPARATOR"

PHP Using variable to call namespace

The answer from @Kris Roofe should work but i would approach it differently with the call_user_func_array function http://php.net/manual/de/function.call-user-func-array.php

Example:

call_user_func_array( $function_as_string.'::'.$method_as_string, $params );

Where $params is an array of data that will be passed to the said function as method params.

So in your example it would be

call_user_func_array('\\interfaces\\'.$class_name.'::get_loginForm');

calling a php namespace with a variable

Use the constant keyword:

$name = constant("fruits\\$color\\redfruit");

You have to escape the slashes.
Also the name has to be fully qualified (relative to the root namespace).

Use variable as namespace

Importing is performed at compile-time.

Assigning the variable is done at run-time after compilation, at which point any import should already be imported.

So this is not possible. Best solution would indeed be to use the FQN in a string and initialize that.

$c = "$user\\$class";

$UserSpecificClass = new $c();

Variable functions with namespaces in PHP

Sure you can, but unfortunately, you need to use call_user_func() to achieve this:

require_once 'template.php';
foreach (array("javascript","script","css") as $tag) {
echo call_user_func('template\\'.$tag);
}

Namespaces in PHP are fairly new. I'm sure that in the future, they will fix it so we won't require call_user_func() anymore.

PHP Pass variables with types from global namespace to included script

Since there's no way for the included file (or your IDE) to know in which context the view was included, there's no way for the IDE to be able to infer what those variables contains. The same view could be included in multiple different contexts, where the variables contains completely different values.

However, you could add a docblock to your views. In the top, add:

/**
* @var \Language\I18n $i18n
* @var \Services\Online\OnlineUser $user
*/

That's telling the IDE what type those variables contain.

Side note

It's generally recommended to avoid using global since it easily can lead to unexpected side effects, and be very hard to debug. There are alternative ways of passing variables to your callbacks, like using use():

Route::add('/', function () use($i18n, $user){
include("../server/Views/Home.php");
});

You can read more about the differences in this answer:

Difference between 'use()' or 'global' to access a global variable in a closure?

PHP Namespace and global variable issue within classes

If you're using an autoloader or include your classes from within another helper function, then the $error variable was never declared in the 'global' scope. It ended up in some local, and got disposed.

Declare it shared right before you assign it a value.

namespace Core;
$class = new ControllerClass();
global $error;
$error = $class->error;

Also while there is nothing wrong with shared variables per se. The name $error seems slightly too generic. Maybe you can up with a less ambigious or more structured exchange variable. $GLOBALS["/var/log"]["controller_error"] or something arrayish.



Related Topics



Leave a reply



Submit