Poppa PHP Library

It suddenly occured to me that I had written a number of handy PHP classes and functions that I was constantly re-using for new projects. It also occured to me that I always had to alter them in one way or another and that I always tried to come up with smarter ways to organize the structure of my classes for every little project.

So I thought I'd better try to create a re-usable structure and fix the classes to become more generic. And that's when PLib was born.

One might think that this is a some what unneccessary job to do since there already is PEAR, Zend Framework and more, and that is of course true, but I also do programming for the pure fun of it and to learn new stuff and that's the only reason why I am working on PLib. And some PLib classes doesn't have an equivalent in other frameowrks so it's not all double work ;)

PLib is released under the GPLv2 license


Subscribe to updates!

What's it all about?

Let me give you a quick example of how it works. Let's generate an RSS feed for our, imaginary, blog.

57 lines of PHP
  1. <?php
  2. require_once('PLib.php');
  3.  
  4. PLib::Import('DB.Database');
  5. PLib::Import('Web.RSS');
  6.  
  7. try {
  8.     $db = DB::Create('mysql://username:password@host/dbname')->Connect();
  9.  
  10.     $mysite = 'http://mysite.com/blog';
  11.  
  12.     $channel = new RSSChannel();
  13.     $channel->title = 'Latest blog posts';
  14.     $channel->link  = $mysite . '/';
  15.     $channel->description = 'Latest blog posts from my fantastic site';
  16.     $channel->image = new RSSImage(array(
  17.         'url'    => $mysite . '/mylogo.png',
  18.         'link'   => $mysite . '/',
  19.         'width'  => '150',
  20.         'height' => '70'
  21.     ));
  22.  
  23.     $sql = "SELECT * FROM blog_entries ORDER BY date DESC LIMIT 10";
  24.     $res = $db->Query($sql);
  25.  
  26.     if ($res->NumRows() > 0) {
  27.         while ($row = $res->Fetch()) {
  28.             $item = new RSSItem();
  29.             $item->title = $row->title;
  30.             $item->link = $mysite . '/entries/' . $row->id;
  31.             // The date will automatically be transformed into a
  32.             // RFC2822-date which  all RSS dates should be formatted as
  33.             $item->{'dc:date'} = $row->date;
  34.             $item->description = $row->excerpt;
  35.  
  36.             // Add the item to the channel
  37.             $channel->AddItem($item);
  38.         }
  39.     }
  40.  
  41.     // Takes two arguments:
  42.     //   version: What RSS version to generate. Default 2.0
  43.     //   encoding: What encoding to use. Default UTF-8
  44.     $writer = new RSSWriter();
  45.     // Add as many namspaces as you whish
  46.     $writer->namespace = array(
  47.         "xmlns:dc" => "http://purl.org/dc/elements/1.1/"
  48.     );
  49.     // If the second argument is "true" an text/xml header will be output - if
  50.     // no headers alerady has been output - and the RSS feed will be echoed out.
  51.     $writer->Render($channel, true);
  52. }
  53. catch (Exception $e) {
  54.     PLib::PrintException($e);
  55.     die;
  56. }
  57. ?>

And that's a pretty easy way to generate an RSS feed I think.

While I'm at it. How about making a thumbnail of an image:

14 lines of PHP
  1. <?php
  2. require_once 'PLib.php';
  3. PLib::Import('Graphics.Image');
  4.  
  5. //! Set up some variables
  6. $imgSrc = '/path/to/image.jpg';
  7. $name   = basename($imgSrc);
  8. $dir    = dirname($imgSrc);
  9.  
  10. //! Create the image object
  11. $img   = new Image($imgSrc);
  12. $thumb = $img->Copy($dirname . '/thumb-' . $name)->Scale(150, 90);
  13. echo $thumb->Path(); # /path/to/thumb-image.jpg
  14. ?>

Easy!

Auto doc

  • PLib::Info() shows a list of all available classes and functions.
  • PLib::Info('ClassName') shows info about the class given as argument.
  • PLib::Info($object) also shows info about the class the object is an instance of.

So do like this:

5 lines of PHP
  1. require_once 'PLib.php';
  2. PLib::Import('Protocols.HTTPClient');
  3.  
  4. $cli = new HTTPClient();
  5. PLib::Info($cli);

If you wan't a full documentation site of PLib just call PLib::PLibDoc() and you will get the same documentation as the documentation on this site.

PLib summary

6 lines of Plain text
  1. php:                  49 pc.
  2. xsl:                  12 pc.
  3. -----------------------------------------------
  4. Number of files:      61 pc.
  5. Directories:          30 pc.
  6. Lines of code:     24902 pc.