Web Programming TutorialsLearn the concept of Autoloading in Zend Framework

Learn the concept of Autoloading in Zend Framework

Learn the concept of Autoloading in Zend Framework 1

In our last tutorial of Zend Framework we learnt to create static pages in Zend Framework, today in this tutorial we will be learning the concept Autoloading in Zend Framework tutorial.

The basic formula to load any class or file into another class or file in PHP is to use include() or require() directives. But if we need the class file in whole application, these include() or require() statements will be repeated in every class or file wherever the desired file is required. To avoid this, the concept of autoloading came into existence.

  • What is Autoloading?
    • Autoloading is a mechanism which eliminates the need to manually include the file.
    • Once an autoloader has been defined, it is automatically called when you are trying to use any class or interface.
    • Using autoloader there is no need to worry about where the class being autoloaded is located in the project, because the autoloader defined will perform the file lookup for us.
  • Zend Framework Autoloading
    • In zend framework, some naming conventions are followed for the class names. These naming conventions setup a relationship between the class names and class files.
    • Here the class name has 1:1 relationship with the file system.
    • The underscore character(“_”) in the class name is replaced by the directory separator (“/”) to resolve the path to the file and a .php extension is put at the end as a suffix. For example, if the class name is Animal_Aquatic_Fish, it will be resolved to its file as Animal/Aquatic/Fish.php.
    • In zend we use a common class prefix say Zend_ as a namespace prefix to prevent naming collisions.
    • By default Zend framework uses Zend_Loader_Autoloader as its autoloader.
    • In simple cases we just require the class and then instantiate it. but as Zend_Loader_Autoloader is singleton, we use getInstance() method to retrieve the class instance as shown below:
    • require_once ‘Zend/Loader/Autoloader.php’;
      Zend_Loader_Autoloader::getInstance();
    • By default, this will allow loading any classes with the class namespace prefix Zend_ or ZendX_, as long as they are on your include_path.
    • But if we have classes with class namespace prefix of something other than Zend_ or ZendX_, they need to be registered using the registerNmaespace() method as shown below:
    • require_once ‘Zend/Loader/Autoloader.php’;
      $loading=Zend_Loader_Autoloader::getInstance();
      $loading->registerNamespace(‘Animal_’);
    • An array of namespaces can also be loaded as shown below:
    • $loading->registerNamespace(array(‘Animal_’,’Bird_’));
    • The Zend_Loader uses a static method loadFile() to load a PHP file dynamically. This method is a wrapper class for the PHP function include() which loads the file.

Thus we had an overview of the autoloading in this Autoloading in Zend Framework tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -