1

I got a PHP library (PHP Markdown) in my library fold in an standard Zend Framework application. What is the best way to load the file and all it's classes to use in my models and controllers.

Structure:

library/phpMarkdown/markdown.php

Note: PHP Markdown has a really ugly structure: It's only real "API" is a simple function, not a class. So the elegant was do not work for this exact case, but regarding the question the genearl solution the correctly named files/class is also "the right answer.

Edit So much good input here, really not sure which answer I should accept! Thanks to you all!

wowpatrick
  • 5,082
  • 15
  • 55
  • 86
  • FYI I used the Textile (not markdown) library and its structure/class name was not directly compatible with Zend Framework autoloading system. I had to put the `Textile.php` file in a `library/Textile/` folder and rename the class inside from `Textile` to `Textile_Textile`. Yup, a bit weird, but it works. Maybe you'll have to do something like that. – Matthieu Napoli Aug 01 '12 at 20:31
  • 1
    For an example of creating a custom autoloader for classes that are not PSR-0 compliant, see http://stackoverflow.com/a/8820536/131824 – David Weinraub Aug 03 '12 at 18:52
  • Thats also really helpful, thanks. Wow so much good input here ... – wowpatrick Aug 04 '12 at 10:08

3 Answers3

2

The autoloader

Just instantiate the class and the autoloader should find it. If it doesn't you need to add the namespace and path.

Mike B
  • 31,886
  • 13
  • 87
  • 111
  • I already had a look at it, but I can't figure it out! Could you give my an example? – wowpatrick Aug 01 '12 at 20:22
  • Sure, can you provide a little more information about your file-structure? – Mike B Aug 01 '12 at 20:31
  • Done. But I found the solution @RockyFord just fine. But if you have anything to add, e.g. for this case you should use this, generally that, for example what is more efficient etc. would be great! – wowpatrick Aug 03 '12 at 12:44
2

If you have a class in the following tree (for exemple) : library/My/Tool.php

You will need to add this to your application.ini :

autoloaderNamespaces[] = "My_"

And then in your code you just call :

$tool = new My_Tool();

Edit :

in the file Tool.php you must follow the Zend Naming Conventions and have something like this :

<?php

class My_Tool {

}

For more informations see this : Zend Naming conventions

Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53
0

To keep it simple and just add that one file, you could put something like this in your Bootstrap.php:

 protected function _initLoad(){
        Zend_Loader::loadFile('markdown.php', '/../library');

    }

I just copied markdown.php into the application library and put this little function in the bootstrap. You could also use Zend_Loader::loadClass(); if you want.

RockyFord
  • 8,529
  • 1
  • 15
  • 21
  • This works just fine. But this means I have global access to all functions/classes in the `markdown.php` and that is loaded every time the application is called? – wowpatrick Aug 03 '12 at 12:45
  • so if you don't need it application wide, you can put the same code in the init() method of any controller or just use it in an action – RockyFord Aug 04 '12 at 06:23