By default, PHP will try to load classes from your current namespace. Refer to the class in the global namespace:
$db = new mysqli(/* ... */);
This is the same thing you'd do when referring to a class in a different namespace:
$foo = new SomeNamespaceFoo();
Note that if you left off the beginning backslash, PHP would try to load the class relative to your current namespace. The following code will look in the namespace ProjectSomeNamespace
for a class named Foo
:
namespace Project;
$foo = new SomeNamespaceFoo();
Alternatively, you can explicitly import namespaces and save yourself ambiguity:
namespace Project;
use Mysqli;
class ProjectClass
{
public static function ProjectClassFunction()
{
$db = new Mysqli(/* ... */);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…