1

I'm trying to use some Zend Components without the MVC structure. For example, if i have this directory structure:

  • /
  • /Zend
  • /images
  • .
  • .
  • /css

    include('Zend/Db.php'); $db = Zend_Db::factory( $db_config['adapter'], $db_config ); //ready to use

But, if I want to have this structure:

  • /
  • /images
  • /Lib
    • Zend
    • OtherLib
  • /css

    include('Lib/Zend/Db.php'); $db = Zend_Db::factory( $db_config['adapter'], $db_config ); //ready to use

But here, it fails because the other files and classes are looking at: / Zend instead of lib / Zend

How can I have access to the Zend_Db class in all of my files, independently of the folder level?

Thanks!

3 Answers 3

4

I would recommend adding the Lib/Zend directory to your include_path. If you do this, the Zend classes will be able to load properly.

Something like:

set_include_path(implode(PATH_SEPARATOR, array(
    '/path/to/Lib',  // <-- change this to the actual path where the Zend dir is uploaded
    get_include_path(),
)));

This adds the Lib directory as the first path in include_path. If you are including other library files more, you may want to move it after get_include_path() so the existing paths are searched first.

Alternatively, you could manually require the Zend files you know you will be using (e.g. Lib/Zend/Db.php, Lib/Zend/Db/Pdo/Mysql.php etc, but this can be tedious and easy to forget so I'd go with the include_path method.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answer my question! I've a file config.php that include in all of my files. (in the first line) . This is the code: set_include_path(implode(PATH_SEPARATOR, array( $_SERVER['DOCUMENT_ROOT'] . '/Lib/Zend', get_include_path(), ))); include 'Zend_Db.php'; ( the include fail ) why?
1

You should add the Lib directory to your include path (not Lib/Zend):

set_include_path(
    $_SERVER['DOCUMENT_ROOT'].'/Lib' . PATH_SEPARATOR .
    get_include_path()
);

You can then use the classes after requiring them:

require_once 'Zend/Db.php';
$db = Zend_Db::factory( $db_config['adapter'], $db_config );

since you said in the comment that you have a common config.php file included by all your scripts, you could use this to setup the ZF autoloader, which makes everything much easier (put this after setting the include path):

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_loader_Autoloader::getInstance();

after that you don't need to include any other ZF files, so you just instantiate the classes as and when you need them. You can also use the same autoloader for loading your own classes from lib, or classes from other libraries.

4 Comments

Thanks, it's working now.. but I've one more question. If I use the Zend_AutoLoader, and create an instance of Zend_Db, $db = new Zend_Db; where I set the database config, tables, hosts, passwords, etc.. ??
You don't need to create an instance of Zend_Db. Just call Zend_Db::factory as you were before. Calling an unknown class triggers the autoloader, which will then require in Zend/Db.php for you.
ohhh it's nice!! thanks so much!! The Autoloader Class, load all of classes? If I don't use all of them, it's not unnecessary?
It simply knows how to translate a class name into a location on the file system. The fact that you are only using Zend_Db at the moment doesn't matter. So you could just require in classes when you need them, but why bother when the autoloader can do it for you?
0

To initialize* ZF do

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

Now you are free to call any ZF component it will get auto-loaded for you automatically .

*Lib directory needs to be part of php include path . Which can easily done as mention by drew010

set_include_path(implode(PATH_SEPARATOR, array(
    '/path/to/Lib',  // <-- change this to the actual path where the Zend dir is uploaded
    get_include_path(),
)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.