How to Use Objects from Other Namespaces and How to Import Namespaces in PHP

How to use objects from other namespaces and how to import namespaces in PHP

If you use:

$obj = new ArrayObject();

it means that ArrayObject is defined in current namespace. You can use this syntax where you are in global namespace (no namespace defined in current scope) or if ArrayObject is defined in the same namespace as current scope (example Foo\Bar).

And if you use:

$obj = new \ArrayObject();

it means that ArrayObject is defined in global namespace.

In your example you probably have code something like that:

namespace Foo\Bar;

$obj = new ArrayObject();

It won't work because you haven't defined ArrayObject in Foo\Bar namespace.

The above code is the same as:

namespace Foo\Bar;

$obj = new \Foo\Bar\ArrayObject();

And if ArrayObject is defined in global namespace (as probably in your case) you need to use code:

namespace Foo\Bar;

$obj = new \ArrayObject();

to accent that ArrayObject is not defined in Foo\Bar namespace;

One more thing - if you use ArrayObject in many places in your current namespace it might be not very convenient to add each time leading backslash. That's why you may import namespace so you could use easier syntax:

namespace Foo\Bar;

use ArrayObject;

$obj = new ArrayObject();

As you see use ArrayObject; was added before creating object to import ArrayObject from global namespace. Using use you don't need to add (and you shouldn't) add leading backslash however it works the same as it were use \ArrayObject; so above code is equivalent logically to:

namespace Foo\Bar;

use \ArrayObject;

$obj = new ArrayObject();

however as I said leading backslash in importing namespaces should not be used. Quoting PHP manual for that:

Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.

Import namespace in PHP

use namespaceproj\lib1 ;

Here you are importing the namespace, not all of the classes inside of that namespace. Therefore, you're trying to create a symbol lib1 pointing towards the namespace namespaceproj\lib1. This fails because you already have a lib1 symbol as a class.

use namespaceproj\lib1  as libnew;

Again, namespaceproj\lib1 is a namespace, not a class. So now you have a symbol named libnew for the namespace and you can instantiate classes using this aliased namespace:

new libnew\lib1;

PHP7 introduced group import syntax, but it still requires you to declare which classes you are importing.

use some\namespace\{ClassA, ClassB, ClassC}

Importing a namespace vs. including files in PHP

use and require_once are completely different things. use is not doing any file importing at all. use is just making your life easier. Instead of writing Full\Path\To\Class every time, you can do

use Full\Path\To\Class

$bar = new Class();

Your are still responsible to include the right files.

Instead of loading all the files by hand, you could rely on PHP auto class loading.

You can use Composer or Frameworks like Symfony 2 or Zend2 which are handling all the autoloading stuff for you.

Migrating existing code to use autoloading and use statements instead of include_once may be very time consuming. There's most likely no search and replace solution.

call different namespace or class from one in PHP

When I echo the class name in __autoload i get the all the class names starting with 'Control\' even when I am calling a class from global namespace.

This is because in Bootstrap.php all the code is in Control namespace (namespace Control). So when you use:

Variables::get_values();

you call

\Control\Variables::get_values();

if you want to use Variables from global namespace you should use here:

\Variables::get_values();

Of course, the same happens for in Variables.php file:

Constructor::load_variables();

As Constructor is defined in global namespace (in class/Constructor.php there is no namespace used), you should access it here using:

\Constructor::load_variables();

If it's still unclear you could also look at this question about namespaces in PHP.

You should also notice that using __autoload is not recommended. You should use spl_autoload_register() now. Documentation about autoloading

PHP how to import all classes from another namespace

This is not possible in PHP.

All you can do is:

namespace Foo;

use Bar;

$obj = new Bar\SomeClassFromBar();

PHP: how to dynamically create class from imported namespace?

Importing is performed at compile-time and there is currently no way to determine the imported aliases. There's a rfc to add such functionallity to reflection, but it seems this will be rejected.

So you have to use the FQN for the class if you want to use them in Reflection or to create them dynamically (http://php.net/manual/en/language.namespaces.importing.php)



Related Topics



Leave a reply



Submit