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
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.
- <?php
- require_once('PLib.php');
- PLib::Import('DB.Database');
- PLib::Import('Web.RSS');
- try {
- $db = DB::Create('mysql://username:password@host/dbname')->Connect();
- $mysite = 'http://mysite.com/blog';
- $channel = new RSSChannel();
- $channel->title = 'Latest blog posts';
- $channel->link = $mysite . '/';
- $channel->description = 'Latest blog posts from my fantastic site';
- $channel->image = new RSSImage(array(
- 'url' => $mysite . '/mylogo.png',
- 'link' => $mysite . '/',
- 'width' => '150',
- 'height' => '70'
- ));
- $sql = "SELECT * FROM blog_entries ORDER BY date DESC LIMIT 10";
- $res = $db->Query($sql);
- if ($res->NumRows() > 0) {
- while ($row = $res->Fetch()) {
- $item = new RSSItem();
- $item->title = $row->title;
- $item->link = $mysite . '/entries/' . $row->id;
- // The date will automatically be transformed into a
- // RFC2822-date which all RSS dates should be formatted as
- $item->{'dc:date'} = $row->date;
- $item->description = $row->excerpt;
- // Add the item to the channel
- $channel->AddItem($item);
- }
- }
- // Takes two arguments:
- // version: What RSS version to generate. Default 2.0
- // encoding: What encoding to use. Default UTF-8
- $writer = new RSSWriter();
- // Add as many namspaces as you whish
- $writer->namespace = array(
- "xmlns:dc" => "http://purl.org/dc/elements/1.1/"
- );
- // If the second argument is "true" an text/xml header will be output - if
- // no headers alerady has been output - and the RSS feed will be echoed out.
- $writer->Render($channel, true);
- }
- catch (Exception $e) {
- PLib::PrintException($e);
- die;
- }
- ?>
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:
- <?php
- require_once 'PLib.php';
- PLib::Import('Graphics.Image');
- //! Set up some variables
- $imgSrc = '/path/to/image.jpg';
- $name = basename($imgSrc);
- $dir = dirname($imgSrc);
- //! Create the image object
- $img = new Image($imgSrc);
- $thumb = $img->Copy($dirname . '/thumb-' . $name)->Scale(150, 90);
- echo $thumb->Path(); # /path/to/thumb-image.jpg
- ?>
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:
- require_once 'PLib.php';
- PLib::Import('Protocols.HTTPClient');
- $cli = new HTTPClient();
- 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
- php: 49 pc.
- xsl: 12 pc.
- -----------------------------------------------
- Number of files: 61 pc.
- Directories: 30 pc.
- Lines of code: 24902 pc.