A taste of projects to come with PHP5

Tweet

Just drawing attention to some interesting projects written specifically for PHP5.

First up is cowiki which, despite being only on release 0.3.x, has already proved a hit and powers the phpCommunity site. The code is very well organised, packed with patterns and it’s clear the author knows Java. There doesn’t seem to any reliance on other PHP libraries so wheels are getting re-invented but, as such, it makes an interesting study for anyone looking for tips on how to put PHP5’s new features into use. It’s a little too Java inspired for my taste for example there’s a base class “Object” – for thoughts on those lines read PHP Application Design Concerns. Each to their own of course and perhaps having some additional abstractions built in helps while PHP5 itself is still a moving target.

Another two interesting projects can be found over at http://www.phpdb.org.

First is Creole, a database abstraction library written specifically for PHP5 and modelled after Java’s JDBC. This will definately pull a Java crowd and the code is, again, very well organised. Here’s a sample of connecting performing a query;


$dsn = "mysql://dbuser:dbpass@localhost/testdb";

$conn = Creole::getConnection($dsn);

$rs = $conn->executeQuery("SELECT id, name FROM users");

while($rs->next()) {
echo $rs->getString("name") . " (" . $rs->getInt("id") . ")n"
}

I get a bit edgy about methods called things like getInt() in PHP. Also I wonder about modelling after JDBC which (my opinion) places too much emphasis on “exceptional” cases (Java being a general purpose language after all) as opposed to being focused on making common cases as easy as possible (dumping a SELECT into an HTML table is a common case in PHP).

By contrast, WACT’s DBC (Database Connection manager) API makes fetching a result set (including connection to database, connection settings stored in an ini file) is reduced to;


$DataSet = & DBC::NewRecordSet('SELECT id, name FROM users');

while ( $DataSet->next() ) {
echo $DataSet->get('name') . " (" . $DataSet->get('id') . ")n"
}

And getting a paged result set looks like;


$PagedDataSet = & DBC::NewPagedRecordSet('SELECT id, name FROM users', $Pager);

Where $Pager is an instance of a “paging component”.

Anyway, such concerns aside, the other project on http://www.phpdb.org is Propel, an Object Relational Mapping tool based on Apache’s Torque (Java). Once your mapping layer is built, it allows you to be blissfully ignorant of SQL (and underlying database abstraction layers) and deal purely with objects. The above examples might become;


$Users = UserPeer::doSelect();

foreach($Users as $User) {
echo $User->getName() . " (" . $User->getId() . ")n"
}

One final project, which you may run into while trawling the Propel documentation is Phing, a build tool for PHP which has much in common with Java’s Ant. That anyone would want to write an Ant-like build tool for PHP (which is already into 2.x version numbers) is a sign that not everyone agrees with the principle that PHP = small web sites only…

These last three projects are coming from developers related to the Binary Cloud I believe, a PHP framework project I wish would expose itself more to the light of day (a select few rave about it while the rest of us hunt for the “Download” link…).

So positive signs of things to come. If more PHP projects follow this kind of lead, then the promise of PHP5 being “Enterprise Ready” will become a reality.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Fab

    There are many OOP PHP frameworks though noone is really ready to use, some projects should merge to be more powerful

  • hlellelid

    Hi Harry,

    Cool to see some of my projects getting air time :)

    Wanted to add a little more info for the Creole evaluation. You’re right that by itself Creole is really not aimed for RAD development. It’s designed to be a clean, fast, but most of all, flexible (in the OO design/interface sense) backend for frameworks (like Propel).

    However … one of the components of the “Creole” project is the “add-on” package called “Jargon” which does try to make the easy stuff easier in PHP. Implementing a lot of the convenience API from DB/MDB and even supporting a basic DataSet and DataObject (Record) type system, it’s a nice add-on to Creole for doing RAD development. You can see the “Jargon Guide” (still in infancy) on http://creole.phpdb.org.

    Here are a few quick examples of what you can do, though:

    $conn = Creole::getConnection($dsn);
    $q = new Query($conn, “SELECT * FROM users”);

    // Query supports DB-style methods:
    // $q->getAll(), $q->getCol()
    // $q->getOne(), $q->getAssoc()

    // or DataSet + Record
    foreach($q->getDataSet() as $Record) {
    $Record->set(“name”, “fred”);
    $Record->save();
    }

    Obviously the DataSet stuff is very basic. Propel provides a much richer system for treating rows as objects.

    As far as the type-methods in Creole like getInt(), etc. I just like programming type-sensitive PHP :) It’s not quite as easy to simply cast, when you have NULL values to account for:

    $intval = (int) $rs->get(“int_col”);
    // if NULL it’s now 0 …

    $intval = $rs->getInt(“int_col”);
    // if NULL it’s still NULL

    … and you can always use $rs->get() if you don’t want any type conversion.

    Hans

  • csshsh

    personally i find it kinda disappointing that propel is designed after torque. torque feels so unclean and just not very modern. why not hibernate? (i know, this is maybe also just a personal choice)

  • http://www.phppatterns.com HarryF

    Hi Hans,

    Thanks for taking time to explain all that. Good to see effort going into building a solid ORM tool for PHP.

    One question that’s not entirely clear to me at the moment, re Propel, is what options does a user have to allow their code to “survive” changes to the database schema? Along the same lines, there was some discussion recently here on the subject of code generation.

  • hlellelid

    Hi Harry,

    Sure, good question re: Propel & generated classes. Propel (and Torque) really take the easy way out. Propel generates empty stub subclasses which is where you put any customizations, validation, new convenience methods, etc. If you change your database schema (e.g. adding or removing columns, changing types, etc.) Propel will generate new *base* classes but will not touch the stub classes. All references in the generated code are to the stub classes; the base classes are abstract and are never referenced directly.

    E.g. generating the classes for the sample “bookstore” project creates several om classes for the “book” table: BaseBookPeer, BookPeer, BaseBook, and Book. You are expected/required to use BookPeer:: and Book classes in your code. These start off as empty subclasses; they exist solely as repositories for custom logic.

    Yes, I’ve read that thread & I partially agree & partially disagree. For an ORM solution, I like generating code because (as someone mentioned in the thread you linked to) you can build stuff that takes advantage of the target database. For instance, several of the databases that Propel supports provide native level support for ON DELETE CASCADE / SET NULL. In order to provide consistent behavior we chose to emulate this logic for MySQL and SQLite. That means that if you are building for MySQL and you are using cascading deletes, additional methods and logic will be added to these classes. Similarly, PostgreSQL requires reading/writing LOB values to be enclosed in transactions, so that can be added to the classes if you are building for Postgres. I’m sure there are other examples, but those are the ones that leap to mind.

    Just generally having generated code is a huge timesaver. Importantly, Propel is not generating code to be modified. The subclass model, IMO, is both simple and elegant and starts from the assumption that you will want to customize behavior, which I think is a safe assumption.

    I would like to make the code generation more customizable through the use of classes. I need a generic PHP source generating framework, basically, so that users can write subclasses to customize the code. Right now we’re using PHP templates, which can be hard to read and difficult to extend without breaking compatibility w/ future Propel versions.

    Hans

  • http://www.phppatterns.com HarryF

    [QUOTE=hlellelid]
    I would like to make the code generation more customizable through the use of classes. I need a generic PHP source generating framework, basically, so that users can write subclasses to customize the code. Right now we’re using PHP templates, which can be hard to read and difficult to extend without breaking compatibility w/ future Propel versions.
    [/QUOTE]

    May be able to be useful there. As far as I’ve seen the most advanced / customizable code generator I’ve seen for PHP is what Jeff has come up with for WACT. The relevant bits are all under here.

    At the moment it’s serving time as a web page template engine (and is fairly tightly embedded) but it “understands” XML (and HTML), the tag names determining which code generation classes get used while attributes help control the objects behaviour. There’s a vague future plan to seperate it off a little and make it useable for something like that XUL to PHP-GTK stuff I was playing with.

    There’s some explaination of how it works in the Template Component Architecture and Tag Developers Guide documents.

    Anyway, might make a useful reference project at least. With PHP5 + DOMXML may be easier to re-implement.

  • hlellelid

    I looked through the WACT template stuff. It does seems like there are aspects of this that could be useful. (I originally though WACT was PHP5 code, but seems not to be.)

    The one thing I really want to avoid, though, is creating a complex metalanguage for creating PHP classes. What I need is a class framework for creating PHP scripts so that, for example, I could simply call $PeerBuilder->buildClass() to create the PHP code for a Peer class. These classes that actually did the building would use another more generic framework for constructing the code. For example the PeerBuilder class would make calls like $this->methods[] = ClassBuilder::addMethod(new Method(“setMyField”, $params)); etc.

    I don’t know if this is clear, but essentially I’m just looking for an OO approach to building PHP classes rather than a new template language. I’m going to spend some more time looking through that template code to see if it has what I need.

    Thanks!
    Hans

  • hlellelid

    re: why Torque & not Hibernate?

    I think Hibernate looks cool. I actually originally did want to use Hibernate as a basis instead of Torque. The main reason I chose Torque is that Hibernate is really built for an environment where *application* state can be maintained.

    From what I saw Hibernate, doing everything at runtime, is slow to initialize. Of course that’s fine in Java, but in PHP where we re-init things every page load it doesn’t work. Sure there are some caching options etc, but it becomes an entirely different kind of application when you have to start managing cache and/or shared memory.

    Also, HQL presents its own problems. Having to parse a SQL-like language (at runtime!) adds an additional huge penalty that could not be easily handled in PHP. While caching may work for the XML files that must be parsed, I don’t see it as a ready option for HQL queries.

    Lastly, Hibernate is only part of what Propel provides: i.e. the runtime framework. I think that the build system is a powerful feature of Propel — particularly for helping w/ rapid application development.

    …. That is not to say that Hibernate does not do many things better. The mapping is much more complex and abstract than Torque/Propel mapping and the relationship & inheritance models are much richer. I think that Torque was a good choice for PHP where simpler is OK given the need for performance (usually above all else). That said, I can’t wait until someone does find a good way to bring the power of Hibernate to PHP.

    HL

  • el_varta

    first of all, it’s not criticism, second i’m not a developer in the proyects mentioned in the note.

    Hans say,
    … I would like to make the code generation more customizable through the use of classes. I need a generic PHP source generating framework, basically, so that users can write subclasses to customize the code. Right now we’re using PHP templates, which can be hard to read and difficult to extend without breaking compatibility w/ future Propel versions….

    Did you saw Metastorage, based on Metabase db abstraction, and MetaL xml based code generator?
    Metabase support lots of db backends:
    Interbase schema management extension class
    Informix schema management extension class Mini-SQL schema management extension class
    Microsoft SQL server schema management extension class
    MySQL schema manager extension class
    Oracle OCI schema management extension class
    ODBC schema management extension class
    Microsoft Access schema management extension class
    PostgreSQL schema management extension class
    SQLite schema management extension class

    http://www.meta-language.net/metastorage.html
    http://www.meta-language.net/
    http://phpclasses.linuxhelp.cl/browse/package/20.html
    i think it’s very good stuff but not very advertised by blogs ;-)
    by the Hans words it’s an step further by the MetaL xml code backend.
    it has php4 generation and php5 advanced.

    I was surfing a lot of articles and the like about db layers and frameworks, propel included, and i am deciding to use Metastorage for db management (Propel handle few db backends, pear DB_DataObject don’t handle datatypes well, only int and string).
    for the framework i will start with MVC, i used to do all by my own (2 frameworks developed) but i think (imo) i must scale to a rock solid pattern like mvc, looking those php implementations i decided for php.MVC. i think it’s the more Struts implemented php mvc out there.
    i will work in integrate the both, i has smarty support included by plugins.

    i think the php comunity needs a serious (imho) framework infraestructure, like java has jakarta-struts/jdbc/velocity, i see it projected with php.MVC/Metastorage/Smarty.

    i don’t have the last word or the trust, i only follow my instincts and my sense of smell.

    good luck all.

  • http://www.mojavelinux.com mojavelinux

    I know that there are a lot of great frameworks out there for PHP, but I have one more I would like to introduce. It is a rather complete and well thoughtout implementation of Struts in PHP, going so far as to emulate JSP, JDBC, and the servlet container. It does vary from Java in many respects, but one could consider it taking the most important aspects for making the port familiar. The project is called Studs MVC Framework+, since it is more than just an MVC implementation.

    http://www.mojavelinux.com/projects/studs/

    I hope you find it useful. It certainly was a tremendous learning experience for me.

  • Anonymous