Enable javascript for live chat

Knowledge Base Overview

ZF classes as stand alone objects

Share on Tumblr

The Zend Framework can be used in two ways: a) as a stand alone library with objects and b) as a web application with a Model-View-Controller (MVC) pattern. Unfortunately there is not much emphasis in the official documentation about it.

In many places the reference guide (manual) fails to mention the subtle differences, i.e. if something applies to the MVC method or only stand alone. Once you know it all it's obvious but until then you are pretty much doomed with it. For instance, the Quick Start guide dives right into the MVC introduction and setup and it does not mention how to use the Zend library stand alone and the classes within as independent objects.

If you like to use the library in Zend Framework stand alone in your existing project this little tutorial will show you how to work with the classes. You can include the library in basically any other PHP project and framework like WordPress, Joomla, Drupal and whatnot.

Stand alone setup

The setup for the Zend Framework as a stand alone library is pretty simple: Get the Zend folder and add the parent folder to your PHP include path. Lets look at this in detail.

Download and move

Download any of the packages in the latest ZF release webpage. Extract the downloaded file. Inside the extracted folder you should find the library/Zend folder. What you need is this Zend folder.

You can copy or move that folder to wherever you like in your web server; or how about a symbolic link. Before you make a decision I recommend that you continue reading, though. It may well be that you already have a perfect location for it.

Include the library

Now, lets look at the PHP environment. It does not really matter where you set this up in your current project but when. You have to set it up before you call your first Zend class.

We will need a path to the parent folder of the Zend folder in the PHP environment which is defined in your PHP include path. If you already have a library or similar folder in your include path—even better. In that case, you just add the Zend folder to that folder and you are all set and don't need the following at all and jump to testing the setup.

If you don't have a library or similar folder yet then you have to create one and add the Zend folder. Then you add this parent library folder of your Zend folder to your PHP path. Note: That is the parent folder, not the Zend folder!

Why the include path?

The Zend classes have a lot dependance between each other, i.e. many classes make use of other classes. For this to work we have require_once() statements all over and these statements use a relative path to the Zend folder. You may already use or want to take a look at autoloading classes but this is not a requirement so I will skip over that. (Note: This will change in ZF2.)

This means that PHP will loop through its path information and concatenate this Zend folder references and look out for the requested file. If you add the Zend folder it will look for the file in a Zend/Zend folder which does not exist! If you run the library on a Linux system be aware of case sensitive nature of Linux, i.e. the folder's name must be Zend not zend.

You can add the include path in your PHP configuration (php.ini file) or at run time. If you want to set this library path directly in your php.ini that's just fine. If not then the following is how to set it up at run time. An index.php is always a great place for this.

// define an absolute path to library directory
// you don't have to set a constant but it is just good practice
// you can also use a variable or add the path directly below
// Note again: the path is the parent of your Zend folder, not the Zend folder itself.
// now set the include path
set_include_path(implode(PATH_SEPARATOR, array(
  APPLICATION_LIBRARY, get_include_path(),

We are almost done.

Test the setup

Finally just some little test if all worked out

// Note: you don't have to use this if statement
// but this would tell you why it is not working
if ( file_exists(APPLICATION_LIBRARY . '/Zend') ) {
  $zf_filter = new Zend_Filter_Word_CamelCaseToDash();
  echo $zf_filter->filter('HelloWorld');
  // echos Hello-World
} else {
  exit('The Zend library folder is missing!');

If you get the Hello-World with the dash you are all set and are able to use the Zend Framework library and the classes as independent objects in your project. If you use an autoloader you should not have to use the initial require statement, but that's another story.

blog comments powered by Disqus