XHTML.net

Technology talks by Loïc d’Anterroches

News, articles, PHP, scripts, XHTML/CSS, …

  1. Home
  2. PHP: Hypertext Preprocessor

Solar PHP framework

The 2007-01-09 at 16:28 by Loïc d'Anterroches filed under PHP: Hypertext Preprocessor.

The Solar PHP Framework is the framework which helped me to create the Pluf Framework. Pluf is heavily inspired by Django with views, a nice ORM and templating system, but what really kicked out Pluf is the Solar idea (pioneered by PEAR as Paul M Jones reminded me) that when you name a class My_Wonderfull_Class the class will be available in the file My/Wonderfull/Class.php. Then with the power of PHP5 you can create an __autoload function (PHP doc of __autoload) to basically automatically load the class based on its name. End result of the autoload trick:

  • No more include_once nightmare in your code, you load the necessary files and only them.
  • A very clean code layout because you know where the classes are.
  • More freedom when using objects because you know that you do not have to define a priori which objects are going to be loaded, you do not need any complex logic to load the corresponding .php files anymore, you use more objects and your code is cleaner.
  • Clean code, good encapsulation of the logic in classes, your code is now easy to read and maintain.
  • Bingo!

Think about it when starting a project in PHP.

Comments from readers

j.ducastel said:

Hi,

If you feel concerned about libraries management and Django, you may be interested by a couple works of mine :

http://webappkit.net which is a packaging system for PHP

and a partial port of a Django template engine which is included as a bonus package.

Oh, et je parle français, dans le civil ;)

Loïc said:

Intéressant. Pour ce qui est du système de templates de Django, j'ai déjà refait une implémentation qui ressemble, il y a même la possibilité d'étendre un template avec un autre :
---
{extends 'base.html'}

{block body}
yop
{/block}
---

Tout est sur : http://code.google.com/p/pluf/

j.ducastel said:

C'est pour ça que j'en parlais...

Je suis parti sur une implementation "stricte" de la syntaxe des templates et des tags django : {% tag %}, {{ var.subvar|filtre }} etc.

Les tags implémentés sont : extends, block, for, if, ifequal, comment, cycle, filter, firstof, plus deux maison, switch et ifincollection. Une partie des filtres est egalement disponible.

Si ça peut vous servir..

Loïc said:

Je vais aller regarder tout cela de plus près ! Merci !

jpic said:

Note that it is possible to use several autoload functions, allowing to autoload PEAR-style classes, in addition to your own autoload implementation http://php.net/spl_autoload_register

Another blog article presents an alternative which allows to name classes and structure the filesystem with a more "lightweight" approach.

Basically, if your framework name (and "namespace") is "mad", you want your framework authentication controller class name to be madAuthenticationController, and probably you would rather store it in mad/contrib/authentication/controllers.php.

Althouht this approach is also more performant, it's only known limit is reached with bloated php apps containing like 500/1000 classes.

http://dev.chocolatpistache.com/blog/2010/05/04/kiss-class-autoloading-php/

Hope that completes your great article.

Voice your ideas

It is painless and I try not to kill electrons in the process.


Your email is required but will not be shared nor displayed.


Do you think your comment will force me to write even better stuff next time? If so, you simply rock.


Logo of Plume CMS