We will be learning the rules for global namespaces and fallback in this Global Namespace and Fallback rules in PHP tutorial.
- We have learned namespaces and how to deal with it in previous tutorials. But still we haven’t reached its basic working.
- Namespaces were introduced in PHP version 5.3 to allow proper organization of classes, functions, interfaces, etc.
- Its purpose is to avoid collision of similar class names by keeping them in different packages.
- It is like packages in java, where you create folders (packages) to store class files.
- It also helps to cut large class names into smaller ones.
- Imagine, to remember a class name before namespaces came into existence you were writing classes as for eg.
- First class:
- Second class:
- But now with namespaces, you can write these classes as:
- First class:
- Second class:
- Here, the reference name for example HotelProject\Dinner in above code is now the namespace and the class name is simply written as Bill or Order.
- The underscore have been converted to backslash(\). This indicates the separation of two namespaces.
- In the above namespace HotelProject\Dinner, HotelProject and Dinner are two namespaces separated by backslash. It has one more relation. The namespace Dinner is the sub-namespace of the namespace HotelProject. This means the classes Bill and Order are inside the sub-namespace Dinner of namespace HotelProject.
- Thus namespaces help a programmer a lot for a systematic and organized work without any collisions.
<?php class HotelProject_Dinner_Bill{ //some code here } ?>
<?php class HotelProject_Dinner_Order{ //some code here } ?>
<?php namespace HotelProject\Dinner; class Bill { //some code here } ?>
<?php namespace HotelProject\Dinner; class Order { //some code here } ?>
- Rules for namespace resolution:
- We have seen that namespaces are defined using namespace keyword.
- All the programs that use classes and functions from these namespaces include them using the use keyword.
- We use unqualified names, qualified names and fully qualified names depending, from where we are calling the functions or classes.
- These names are resolved to their fully qualified names so that the desired function or class is called at the desired place.
- There is a processing mechanism, which references all the classes and functions in the source code and replaces them with their fully qualified names at the compile time itself.
- So before the actual execution of the code the unqualified names and qualified names are replaced with their fully qualified names.
- But how is it done, let us see:
- Every name which does not start with a backslash (\) is considered to be present in the same namespace in which the program is been written.
- For eg, see the code below:
1) Use of unqualified name:
<?php namespace HotelProject\Dinner; //call to a method in class Order $o=new Order; $o->receive(); ?>
2) Use of qualified name:
<?php namespace HotelProject; //call to a method in class Bill $b=new Dinner\Bill(); $b->generate(); ?>
<?php namespace App; require_once("app1.php"); require_once("app2.php"); $o=new \App\Lib2\call(); echo $o->receive(); ?>
- We know that the built –in functions and classes defined in PHP prior to version 5.3 don’t have any namespace. They are globally available everywhere.
- But the resolution rules of namespace tell us something different. The rule says that the global functions and classes should start with a backslash (\).
- If we follow this rule for the built-in classes and functions, we will have to write time() function as \time(), strtolower() as \strtolower(), strlen() as \strlen() etc.
- This will make our code unreadable; seem difficult with lot of symbols.
- So the built-in classes and functions which were developed prior to PHP version 5.3 falls back to the resolution rule of global namespace and are an exception to the resolution rule of namespace.
- These functions and classes can be directly used anywhere by just using their direct names like time(), strtolower(), strtoupper(), strlen() etc.
- But this fallback rule does not apply to some classes and interfaces like Iterator and ArrayObject. They have to be written as \Iterator and \ArrayObject respectively, otherwise they won’t work.
- We now know that the namespace names are resolved at compile time itself. But we can also call the functions, classes etc. in a namespace dynamically at run time.
- This happens when we store the namespace names in a variable or pass it as a parameter to a function.
- Calling any class or function dynamically is possible only when we specify its fully qualified name.
- Let us see an example of our HotelProject:
<?php namespace N1; $base=’HotelProject’; $sub=’Dinner’; $class=’Order’; //making a fully qualified name of call Order $name=$base.’\\’.$sub.’\\’.$class; //making an object of class Order $o=new $name; $o->receive(); //calling function from Order class ?>
Thus we finished learning interesting global namespace, fallback rules and dynamic feature of namespaces in this Global Namespace and Fallback rules in PHP tutorial.