Search code examples
phpzend-frameworkautoloadrequire-once

Using Zend_PDF in a stand alone mode, how to set the include of the land class


I'm writing a tiny school project in php. I needed to display some information in a pdf format and send the file via mail.

When i've copy the PDF folder with the pdf class file from the librairy.here is my folder structure

  /
    /lib
      /Swift
        /....
      /Zend
        /Pdf
        Pdf.php
    test_file.php
   

here is the content of test_file.php

require_once 'lib/Zend/Pdf.php';
$pdf = new Zend_Pdf();
$pdf->render();

and it's throwing this

( ! ) Fatal error: require_once() [function.require]: Failed opening required 'Zend/Pdf/Page.php' (include_path='.;C:\xampp\php\PEAR;C:\ZendFramework-1.10.8\bin;') in C:\xampp\htdocs\schoolproject\lib\Zend\Pdf.php on line 27

but i did notice that all classes includes by referring to the top Zend folder, even siblings classes ex :

require_once 'Zend/Pdf/Page.php';

i'm a little confuse about how to deal with that. I'm think about autoload feature or manually correct the require path to suit my project (which will be a pain).

What's the best way to go around it?

THanks for reading this.


Solution

  • You have to register the ZF autoloader. After that you can just use the classes and the autoloader will figure out the rest.

    $zf_path = 'PATH/TO/YOUR/LIB/FOLDER';
    set_include_path($zf_path.PATH_SEPARATOR.get_include_path());
    require_once($zf_path.'/Zend/Loader/Autoloader.php');
    $loader = Zend_Loader_Autoloader::getInstance();
    $loader->registerNamespace('Zend_');