Can PHP namespaces contain variables?

PhpVariablesNamespaces

Php Problem Overview


Can PHP namespaces contain variables? If so, how can this be accomplished?

Php Solutions


Solution 1 - Php

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

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"

Solution 2 - Php

Try this

<?php
namespace App\login; 

$p = 'login';
$test2 = '\App\\'.$p.'\\MyClass';

$test = new $test2;

Solution 3 - Php

No they cannot, as mario said.

To encapsulate variables use Classes. Polluting the global variable space should definitely be avoided.

  • Example

class_dbworker.php:

    class DbWorker
    {
        //properties and method logic
    }

    class DbWorkerData
    {
        public static $hugerelationsmap = array(....);
        public static ....
    }

mainapp.php:

    include_once 'class_dbworker.php';
    print_r( DbWorkerData::$hugerelationsmap );
  • Example using namespaces

class_dbworker.php:

    namespace staticdata;
    class DbWorker
    {
        //properties and method logic
    }

    class DbWorkerData
    {
        public static $hugerelationsmap = array(....);
        public static ....
    }

mainapp.php:

    include_once 'class_dbworker.php';
    
    use staticdata as data;

    print_r( \data\DbWorkerData::$hugerelationsmap );

Solution 4 - Php

It is not possible because $MYVARNAME is still in the global scope. Try following code.

namespace.php

<?php
	namespace MYNAME;
	use MYNAME as M;
	const MYVAR   = 'MYVARNAME';

	${M\MYVAR}    = date('Y');
	echo $MYVARNAME;  // PRINT YEAR
	$MYVARNAME    = 'X';
	echo $MYVARNAME;  // PRINT X
	echo ${M\MYVAR} ; // PRINT X

	include('file.php');
?>

file.php

<?php
    ${MYNAME\MYVAR}=date('Y');
    echo $MYVARNAME;        // PRINT YEAR
    $MYVARNAME = 'X';
    
    echo $MYVARNAME;        // PRINT X
    echo ${MYNAME\MYVAR};   // PRINT X
    
    include('file2.php');
?>

file2.php

<?php
	namespace MYNAME2;
	use MYNAME2 as N;
	const MYVAR   = 'MYVARNAME';

	${N\MYVAR}    = 'Y';
	echo $MYVARNAME;  // PRINT Y
	echo ${MYNAME\MYVAR}; /* PRINT Fatal error: Uncaught Error:
	Undefined constant 'MYNAME2\MYNAME\MYVAR' */
?>

Solution 5 - Php

It can be done - sort of.

This is probably extremely bad and should never be done, but it is possible by using variable variables, and the magic constant for namespace. So a string-variable to name the variable we want to use, like so:

<?php
namespace your\namespace;

$varname = __NAMESPACE__.'\your_variablename'; //__NAMESPACE__ is a magic constant
$namespaced_variable = $$varname; //Note the double dollar, a variable variable
?>

Solution 6 - Php

Store Complete classPath in Variable and use after 'new'.

It is very important to realize that because the backslash is used as an escape character within strings, it should always be doubled when used inside a string.

<?php
$a = "namespace\\className"; // 'which will print namespace/className'
$obj = new $a;
?>

Solution 7 - Php

You can bound a variable to the namespace by wrapping the variable inside a function.

<?php
 namespace furniture;
// instead of declaring a $version global variable, wrap it inside a function
function version(){
 return "1.3.4";
}
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmpireJonesView Question on Stackoverflow
Solution 1 - PhpmarioView Answer on Stackoverflow
Solution 2 - PhpMaximView Answer on Stackoverflow
Solution 3 - PhpLorenz Lo SauerView Answer on Stackoverflow
Solution 4 - PhpcitygeistView Answer on Stackoverflow
Solution 5 - PhpJohnFView Answer on Stackoverflow
Solution 6 - PhpAsif AliView Answer on Stackoverflow
Solution 7 - PhpTinsaeView Answer on Stackoverflow