0

Previously, i am using zend 1.x. I am able to autoload zend classes using the below code.

// autoload class from Zend Lib
require_once ABSPATH.'/classes/Zend/Loader/Autoloader.php'; 
$loader = Zend_Loader_Autoloader::getInstance();    
 try{
// database connection      
$dbo = Zend_Db::factory('pdo_mysql', array( 
        'host'     => DB_HOST, 
        'username' => DB_USER, 
        'password' => DB_PW, 
        'dbname'   => DB_PREFIX.DB_NAME
    )); 
$dbo->getConnection();
// save database adapter for easy usage in other classes
Zend_Db_Table::setDefaultAdapter($dbo);
Zend_Registry::set('db', $dbo);

}catch(Zend_Db_Adapter_Exception $e){
print $e;
 //header("Location: http://www.google.com/error/");
}

I am upgrading to zend 2 as the classes might be better. May i know how do i autoload them?

Slay
  • 1,285
  • 4
  • 20
  • 44
  • hi! i am not using zend framework, however, i am using their classes as a standalone in my custom framework.. – Slay Jul 13 '13 at 06:04

3 Answers3

2

If you are just using ZF2 as a standalone library without employing the full MVC framework, then autoloading is fairly straightforward:

  1. Make sure that the Zend directory is on your php include_path.
  2. Push an autoloader using spl_autoload_register

The following is essentially what Zend\Loader\StandardAutoloader::loadClass() does when functioning as a fallback autloader:

spl_autoload_register(function($class) {
    $f = stream_resolve_include_path(str_replace('\\', '/', $class) . '.php');
    if ($f !== false) {
        return include $f;
    }
    return false;
});

This would use the PSR-1 autloading mechanism for all classes, not just the Zend classes.

Alternatively, you could just do the following:

require_once 'Zend/Loader/StandardAutoloader.php';
$autoloader = new Zend\Loader\StandardAutoloader(array(
    'fallback_autoloader' => true,
));
$autoloader->register();

As above, this will apply PSR-1 autoloading to all classes. If you want this mechanism for the Zend classes only, then pass 'fallback_autoloader' => false.

David Weinraub
  • 14,144
  • 4
  • 42
  • 64
  • hi, i am adding the above code in, new Zend\Loader\StandardAutoloader give me a syntax error. – Slay Jul 15 '13 at 03:59
  • 1
    Correcting the `require_once` call. Had a slash instead of a dot. – David Weinraub Jul 15 '13 at 05:56
  • 1
    Another thought: ZF2 requires at least PHP 5.3. Can you confirm by running `php --version`? – David Weinraub Jul 15 '13 at 06:19
  • thanks! the autoloader works, another important questions. how do i load zend_db classes like the code as shown? $dbo = Zend_Db::factory('pdo_mysql', array( 'host' => DB_HOST, 'username' => DB_USER, 'password' => DB_PW, 'dbname' => DB_PREFIX.DB_NAME )); $dbo->getConnection(); – Slay Jul 16 '13 at 08:54
  • 1
    This code - referencing `Zend_Db` - looks like ZF1. Using both ZF1 and ZF2 together is possible, but it is a whole different issue. See http://framework.zend.com/manual/2.1/en/migration/zf1_zf2_parallel.html – David Weinraub Jul 17 '13 at 14:56
  • is it advisable to use both? what is zf1 and zf2? is zf2 better than zf1? – Slay Jul 18 '13 at 06:54
  • ZF stands for "Zend Framework". I would choose to use ZF2 only. – David Weinraub Jul 18 '13 at 14:11
  • Starting to get really out of SO scope here. I prefer ZF2 largely because of PHP 5.3 namespaces which I find more elegant than ZF1's PEAR-style prefixing scheme. – David Weinraub Jul 18 '13 at 14:57
  • Also, per-component use via Composer. – David Weinraub Jul 20 '13 at 16:44
  • 1
    Thanks @DavidWeinraub, I thinks there is an extra ")" at the end of: spl_autoload_register(function($class)) – leticia Aug 12 '13 at 12:31
1

You can also load the individual components through Composer and then you just need to include require 'vendor/autoload.php'; at the start of your execution script.

Adrian
  • 1,370
  • 11
  • 21
0

I would Suggest you to study a skeleton App Provided by ZF2, I am not much aware About ZF1 but as far as i know ZF2 has its own mechanism of Auto Loading, as far as Auto Loading is concerned when you follow Skeleton Application you will notice that there is a Config\application.config.php this is the file where we load All the Modules etc. As a sample i will load my file below. As far as setting connections etc is concerned you will find them under them same directory tree i.e "Config\local or Config\Global".

 <?php
return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
    'ZendDeveloperTools',
    'DoctrineModule',
    'DoctrineORMModule',
    'Application',
    'Administration',
    'Account',
    'Manufacturing',
    'GridMain',
),

// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => array(
    'module_paths' => array(
        './module',
        './vendor',
    ),
    'config_glob_paths' => array(
        'config/autoload/{,*.}{global,local}.php',
    ),
),


);
noobie-php
  • 6,817
  • 15
  • 54
  • 101