Can PHP namespaces contain variables?

Try this

<?php
namespace App\login; 

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

$test = new $test2;

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"