Welcome to my Blog

I am Nelson Desai. I complete computer engineering.

This is my personal blog. Here I share some post, which can helpful for me, my friends and you.

Some post may be illegal to share like some crack and keys. but, i think.. which things put online then it's not be secure any more whatever company try.

So, I requested you pls download and use it at your risk. and don't upload it again online.

Thank You for peeping my blog.. Have a Good Day!

Showing posts with label zend autoloader. Show all posts
Showing posts with label zend autoloader. Show all posts

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


How to use zend autoloader ??

zend autoloader tutorial >>

File Structure:
library

       |--- Amazon


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

In Bootstrap.php
public function _initAutoloaders()
{
    $loader = Zend_Loader_Autoloader::getInstance();
    $loader->registerNamespace('Amazon_');
}

In IndexController or any..
$Amazon_Action = new Amazon_Action();


Now use custom file structure:
In application

   classes
       |--- googles
             |--- nels
                    |--- action.php [with calss name My_Classe_Google_Nel_Action]

             |--- test.php [with class My_Classe_Google_Test] 

       |---demo.php [with class My_Classe_Demo]

In Bootstrap.php
public function _initAutoloaders()
{
 $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
      'basePath' =>  APPLICATION_PATH . '/',
         'namespace' => 'My',
     ));
 
 $resourceLoader->addResourceType('demo','classes/','Classe');

 $resourceLoader->addResourceType('test', 'classes/googles/', 'Classe_Google');

 $resourceLoader->addResourceType('action', 'classes/googles/nels/', 'Classe_Google_Nel');
 
 }

In Controller
$opt1 =  new My_Classe_Google_Nel_Action();
$opt2 = new My_Classe_Google_Test();
$opt3 = new  My_Classe_Demo();
For more advance use of autoloader follow thins link..
http://nelsondesai.blogspot.in/2012/09/advanced-use-of-autoloader-in-zend.html
http://framework.zend.com/manual/1.12/en/learning.autoloading.resources.html

For more details mail me on nelson.desai@gmail.com or comments here.


--
Thank You
Nelson Desai

Twitter Delicious Facebook Digg Stumbleupon Favorites More

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