·  PHP – Syntax – Keywords – namespace

 

- Info:

  - If used, namespace declaration has to be at the top of the script.

    Other namespace declarations can then be placed anywhere in the script but advice is to use single declaration per file.

  - Namespace defines prefix which will be attached to all constants, function, classes or interfaces below the declaration to

    create their full names:

    namespace ivoronline\test;

    const FILENAME = "Extra.php";     #Full const name is now: \ivoronline\test\FILENAME

  - Exception are variables which will always exist in the global scope, they are never bound to namespaces.

  - This tutorial is based on:

    - http://www.php.net/manual/en/language.namespaces.faq.php

    - http://stackoverflow.com/questions/5287315/can-php-namespaces-contain-variables

    - http://www.sanisoft.com/blog/2010/07/19/namespaces-in-php-5-3-part-1/

    - http://www.phparch.com/2010/03/namespaces-in-php/

    - http://php.net/manual/en/language.namespaces.definitionmultiple.php

 

- Why namespaces:

  - Namespaces solve following problem.

  - If you want to include Extra.php into Main.php, and they have some identical constant names, conflict occurs.

    When such cosntant is referenced PHP will not now which variable to use.

  - By defining namespace at the begining of at least one of these files this problem dissapears.

    This is so because before Extra.php is copied into Main.php full coinstant names are created by using namespace as

    prefix for each constant in both files.

 

- Traditional Solution:

  - Traditionaly programers tried to avoid such conflicts by giving long names to their constants.

  - They would prefix names with something unique, like their domain name, to make sure others don't accedantely name

    their constant the same way.

  - Namespaces create such long names automaticly since namespace will be attached to every constant in your script when     you reference it.

  - Unfortunately the problem remains when such script is included into another script with different namespace.

    Then to reference constant fom included script you need to use its full name, the one that contains complete namespace.

    This problem can be partialy solved by using keyword use which can create shorter alias for class or namespace.

    Unfortunately exact alias has to be defined for each class.

    You can't write short class name and tell PHP to try to find it in suplied list of namespaces, like you can in JAVA.

 

- Without namespaces:

  - This example demonstrates problem which occurs if namespaces are not used.

  - We can't reference FILENAME from Extra.php since it is overriden by constant with the same name in Main.php.

 

    Extra.php

    <?php

      const FILENAME = "Extra.php";

    ?>

 

    Main.php

    <?php

      include("Extra.php"); #This statement will be replaced by content of Extra.php.

      const FILENAME = "Main.php";

      echo(FILENAME);       #"Main.php" We want to call FILENAME from Extra.php but can't reference it.

    ?>

 

     Resulting.php

    <?php

      const FILENAME = "Extra.php"; #FILENAME from Extra.php.

      const FILENAME = "Main.php"#FILENAME from Extra.php is no overriden by $var from Main.php.

      echo(FILENAME);        #"Main.php" We want to call FILENAME from Extra.php but can't reference it.

    ?>

 

- With namespaces:

  - This example demonstrates how the above problem can be solved by using namespaces.

  - We can now reference FILENAME from Extra.php by using it's full name which contains namespace as prefix.

 

    Extra.php

    <?php

      namespace ivoronline\test;

      const FILENAME = "Extra.php";     #Full const name is now: \ivoronline\test\FILENAME

    ?>

 

    Main.php

    <?php

      include("Extra.php"); #This statement will be replaced by content of Extra.php.

      const FILENAME = "Main.php";

      echo(FILENAME);       #"Main.php" We want to call FILENAME from Extra.php but can't reference it.

    ?>

 

     Resulting.php

    <?php

      const FILENAME = "Extra.php";     #FILENAME from Extra.php.

      const FILENAME = "Main.php";      #FILENAME from Extra.php is no overriden by $var from Main.php.

      echo(\ivoronline\test\FILENAME);  #"Extra.php" We can now reference FILENAME from Extra.php.

    ?>