Advanced use of autoloader in zend


For read simple use of autoloader you can see my previous post here on below link:
http://nelsondesai.blogspot.in/2012/09/how-to-use-zend-autoloader.html

Now Advance Use of autoloader for cloud base database service or any.

Que: I have two different class [one amazon cloud and second azure cloud] with same functions name like [insert, update, join etc].

Notes: both class must have same function.

Now i want to choose database[class] dynamic.

So, it's not good to go every controller and we change a class name where we use a class object.

Solution is here,

Let's start..
first file structure..  default I always prefer to use library folder.

library

        |--- Amazon
                   |--- Action.php [with class Amazon_Action] 


        |--- Azure
                   |--- Action.php [with class Azure_Action]


In solution you can also use a custom folder replace of library like [application/cloud/] folder..
for custom folder read my previous post Click Here.

Now in index.php

just define which cloud db we want to use..
define('_APP_BOOT_LIB','Amazon_');
Now in application Bootstrap.php
public function _initAutoloaders()
 {
        $autoloader = Zend_Loader_Autoloader::getInstance();
       
        $autoloader->registerNamespace(_APP_BOOT_LIB);
        if(_APP_BOOT_LIB == "Amazon_")
            $objUser = new Amazon_Action();
        else if(_APP_BOOT_LIB == "Azure_")
            $objUser = new Azure_Action();

     //Here two way to store object 
     
       Zend_Registry::set('objUser', $objUser); 
//for get objUser in controller[$objUser = Zend_Registry::get('objUser');]
     // $this->objUser = $objUser;  
//for get objUser in controller[$objUser = $this->_invokeArgs['bootstrap']->objUser;]
}

Now in controller  IndexController.php
we can get back object to use...
$objUser = Zend_Registry::get('objUser');    
//$objUser = $this->_invokeArgs['bootstrap']->objUser;


 //Notes: both class must have same function.. 
$objUser->query($params);
$objUser->insert($params);

Gud Luck..

For more details mail me on nelson.desai@gmail.com or comments here.
--
Thank You
Nelson Desai


1 comments:

geo4pak said...

good job

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by NDR Group | Bloggerized by Er. Nelson Desai | Love U My Friends