Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does use statement order affect functionality in PHP?

Tags:

namespaces

php

I've been using PHP's namespaces for some time now and I think it is a great addition to my programming. This morning I wondered about something concerning the use statement. I'm wondering if the order of use affects the functonality of my PHP code.

According to PHP.net

The ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces. This is similar to the ability of unix-based filesystems to create symbolic links to a file or to a directory.

AIn PHP, aliasing is accomplished with the use operator.

~ That sucks, nothing about the order of inclusion. Let's ask my friends on SO!

Example

Below, I'll try to give a better example

Class C

namespace Fully\Qualified\Namespace;

use Fully\Qualified\Namespace\B;
use Fully\Qualified\Namespace\A;

class C
{
    // ...
}

Class B

namespace Fully\Qualified\Namespace;

use Fully\Qualified\Namespace\A;

class B extends A
{
    // ...
}

Class A

namespace Fully\Qualified\Namespace;

class A
{
    // ...
}

Now, will it give me trouble that class B is included before class A in my use statements?

like image 308
Peter Avatar asked Oct 19 '22 23:10

Peter


1 Answers

When alias namespaces no.

When used in the class body for Traits may affect in certain scenarios related with the use of docblock annotations.

like image 165
Maks3w Avatar answered Oct 27 '22 10:10

Maks3w