Global Namespace and Fallback rules in PHP

0
3146

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.
    1. First class:
    2. Second class:
  • But now with namespaces, you can write these classes as:
    1. First class:
    2. 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.
  • 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:

      • Here, we know that the above code is included in the namespace HotelProject\Dinner. The class Order is also written inside the same namespace. So we can call it directly with its name Order (unqualified name) and use it.
      • 2) Use of qualified name:

      • Here, we know that the above code is included in HotelProject namespace. But the class Bill is inside the sub-namespace Dinner of namespace HotelProject. We are already inside HotelProject namespace and just have to reach Bill class. So we call Bill class by its qualified name Dinner\Bill.
      • Such unqualified and qualified names are replaced with their fully qualified names at compile time.
      • Like the statements $o=new Order; and $b=new Dinner\Bill(); will be replaced with $o=new HotelProject\Dinner\Order() and $b=new HotelProject\Dinner\Bill(); respectively.
      • Next rule is that the names which start with backslash (\), belong to the global namespace (root directory) and they are resolved to their literal specific value.
      • For example see the following code:
      • The above code is included in namespace App. We know that we are inside namespace App, still we called the class call as $o=new \App\Lib2\call();
      • Because we know that the names starting with backslash are relative to the global namespace. The name \App\Lib2\call() will be converted to App\Lib2\call() at compile time.
  • Fallback:
    • 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.
  • Dynamic feature:
    • 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:
    • In the above code we are trying to call the class Order in HotelProject\Dinner namespace dynamically.
    • This code is included in namespace N1. We have 3 variables, $base, $sub and $class which contain values HotelProject, Dinner and Order respectively.
    • A string $name is assigned a fully qualified name of Order class using the variables separated with double backslashes (\\).
    • Why is double backslash needed? The rule is to use single backslash. This is because, we need to escape the backslash while storing the name in a string. This doesn’t give any error in the namespace path/name.
    • Then an object of Order class is created using $o=new $name; statement.
    • The values of variables are evaluated at run time, hence it is called as dynamic calling of a function/class in a namespace.

Thus we finished learning interesting global namespace, fallback rules and dynamic feature of namespaces in this Global Namespace and Fallback rules in PHP tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here