Feb
03
This is probably the one reason why everyone should switch to PHP 5! According to Charl it is “teh shit”. It is the __autoload() function. You define an __autoload() function and by calling this function the scripting engine is given a last chance to load the class or include the class file before PHP fails with an error.
I’ve created this very simple example to demostrate the effectiveness of the __autoload() function. In the index.php file, I define my __autoload() function and tells it to include a file with the same name as the class. In other words, my class name is oneClass() so the file which contain oneClass() is called oneclass.php
//The magical autoload function...
function __autoload($classname)
{
require_once $classname . '.php';
}
Now I initialize class two :
$class2 = new twoClass('Second class');
$class2->getText();
PHP will require and include the file called twoclass.php without me doing anything. Nothing funny here… Thing is within class two I initialize class one. As you can see, nowhere did I say it should be included! So withing class two it autoloads class one and initialize it.
class twoClass
{
private $text;
function __construct($txt)
{
$this->text = $txt;
}
function getText()
{
$class = new oneClass($this->text);
$class->getText();
}
}
w00t!!! Nifty, hey? Note that I’ve create the page second.php to demonstrate how the same would result in an error due to the fact that I did not require class one. If you’d like to check it in action, feel free to download this example here.
Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
PHP Stii
Feb
03
PHP 4 reached its end of life the 31st December 2007. They will continue to fix bugs up until 08 August 2008. Yet, most of the biggest CMS software, guys like Joomla!, Drupal and Wordpress keep on supporting PHP 4 instead of PHP 5. Why on earth?! I do get it that you cannot predict what version of PHP would be on a shared host, but the hosting providers should pull up their socks and upgrade!
PHP 5 is a 1000% improvement on 4 and has been out for a couple of years, so why would some shared hosts still cling to PHP 4? If these guys like Joomla!, Drupal and Wordpress develop in PHP 5, surely hosting companies will be forced to upgrade their PHP version? It is not like they would suddenly break all PHP 4 code as 5 is perfectly backward compatible. So what is the issue? When will this software be rewritten in PHP 5? Hehehe, that would be quite a feat, except that Joomla! 1.5 was written almost from the ground up not all that long ago!
Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
PHP, Programming Stii
Feb
01
Still, it is lightweight and clean. Kohana is a Code Igniter fork that is completely open source and community driven. If you are used to Code Igniter, then Kohana would be a walk in the park. The one major difference is that Kohana is exclusively for PHP 5 while Code Igniter supports from PHP 4 and up. Kohana is strinctly OOP with benefits like visibility protection, automatic class loading, overloading, interfaces, abstracts, and singletons. Cool!
Whats more is that while Code Igniter supports only $POST arrays and no GET, Kohana lets POST and GET arrays live together in harmony with the same tight security of Code Igniter.
Another trivial thing thats always kind of bugged me of Code Igniter is the directory structure. Kohana’s directory structure makes a lot more sense. See, Code Igniter has one directory at the root called System. In this directory is a bunch of subdirectories of which your application directory is a part of. Kohana went and put the application directory with the system directory on the root path. I know, I know, it really is pedantic of me, but it just makes so much more sense to do something so insignificant. Well, actually, on second thought, its not as trivial as it might seem. Come upgrade time, you only need to upgrade the system folder and your application folder would be untouched. Code Igniter stores a bunch of system related files in their app folder which could get overwritten when upgrading, but need to be upgrade every once in a while as they do make changes to those files occasionally.
Another thing is that changes and fixes happens a lot faster in Kohana as it is a community project. Code Igniter is brilliant, but it was kind of slowish in releasing fixes and improvements as Ellis Labs first do Expression Engine (it seems) then Code Igniter. Kohana living in a community allows for rapid expansion and fixes.
All in all, I think Kohana is a great fork on an already great framework. Keep up the good work. If you are worried about it only working on PHP 5, well, PHP 4’s end of life was the 31st of December 2007 already! Convert! At least bugs will be fixed till 2008-08-08. Cool date they chose!
Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
Code Igniter, Open Source, PHP, Programming, web development Stii
Dec
06
Every programming language seem to have some sort of Ruby on Rails like framework. PHP got a couple lot. Python got Django (Django is not Rails though). Now the BBC went and made a Rails-like web framework with PERL.
This I found quite amusing:
“Like most organisations the BBC has its own technical ecosystem; the BBC’s is pretty much restricted to Perl and static files. This means that the vast majority of the BBC’s website is statically published - in other words HTML is created internally and FTP’ed to the web servers. There are then a range of Perl scripts that are used to provide additional functionality and interactivity.”
Their sites are statically published?! I did not know that and find it quite, uhm, shocking that a big organization would have such a primitive way of publishing their sites.
“For applications that run internally we use Ruby on Rail. Because we enjoy using it…”
No surprise there.
They built PERL on Rails because:
“We need to use Perl, there are restrictions on which libraries can and can’t be installed on the live environment and we needed a framework that could handle significant load.”
Now I’m just wondering, are they planning to release the source code, or is it going to stay an internal framework exclusive to the BBC? Should be quite interesting to see it and experiment with it. No-one knows yet. This part of their site is powered by PERL on Rails, they say.
They would almost certainly have to change the name. When PHP on Rails first saw the light, it was quickly slapped with a lawyers letter and had to change its name. Now known as PHP on Trax.
Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
Open Source, PHP, Programming, Python, Ruby (on Rails), Software Development, web development Stii
Nov
14
I feel like I’ve been in the stone age the latter part of this year, but saw tonight that the new release of Mambo (version 5) is going to be powered by CakePHP. Wow! Interesting move. If you do not know this, Joomla! used to be Mambo and a lot of the Joomla! version 1.0.x code is still like mosMainBody or mosLoadModule. mos as in Mambo Open Source. That was until there was some serious disagreement about corporate influence by the company who funded and founded Mambo, Miro International. You can read more about that saga here. Ever since the fork between Mambo and Joomla! it has been Joomla! pretty much all the way for me.
What is kind of strategic interesting is that Mambo nr 5 will be powered by a great (Open Source) PHP MVC framework called CakePHP. I’m undoubtedly a Code Igniter fan, but Cake, although more complex/heavyweight, is a pretty good MVC framework! So I think this is a very clever/sensible move on Mambo’s side. Joomla! 1.5 reinvented the MVC wheel by developing their own MVC framework for developing plug-ins. This means that although you have been developing Joomla! components up to now, you will have to learn the MVC side of 1.5 from scratch if you want to use it.
Mambo users, both old and new can start playing with CakePHP and get a feel for how it would work in Nr 5. They might even win a couple of new supporters with this move to CakePHP. I’m dead sure the old supporters would not mind either as the API was starting to feel a bit outdated anyway.
I know this goes without saying, but it is less obvious given the nature of how Cake works, and that is whether existing Mambo components would be completely backward compatible. I suppose that would depend on how deep the Cake integration is going to go. It would be suicide to throw away all the existing component code! Thats a no-no. Then again, as I said, it goes without saying! Joomla! 1.5 has its own new MVC built in with funky ways to do new components, but they made damn sure they cater for existing ones to some extent. Lets see how Mambo handles it.
What would be interesting to see is whether Mambo would be powered by Cake or whether Mambo would become a CMS built for Cake…

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
PHP, Software Development, web development Stii
Oct
18
I’m a bit perplexed why you can sort an array with keys in PHP and you can randomize an array with no keys, but you cannot randomize an array with keys. Weird…
If you have to randomize an array with keys, here is a way to do it. Would have been nice to have been able to simply say:
shuffle($arrayWithKeys);
but thats not to be as you would lose the keys.
So what you need to do is:
//Make an array without keys of the keys of your associative array
$keys = array_keys($arrayWithKeys);
//Shuffle the keys up
shuffle($keys);
//New array which is the one you would use or return
$newArrayWithKeys = array();
foreach($keys as $k) {
//The array items would be packed into the new array in the order that it was shuffled.
$newArrayWithKeys[] = $arrayWithKeys[$key];
}
Thanks to Sajith for the tip!
Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
PHP, Programming Stii
Oct
07
I’m not a big fan of Smarty Templates in PHP. This is only the second project in a short space of time I’ve been required to use it due to existing code already implementing Smarty. The project is on a hectic deadline and we’ve been held up by unforeseen circumstances, but I know its no excuse! I assumed (yes… I know what they say about assumptions…) that Smarty’s date specifiers would be the same as PHP’s. *NOTE* That is not the case. PHP got its own style of specifiers while Smarty uses C style specifiers. It is also exactly the same as MySQL’s date specifiers.
Let say you get a datetime value out of your MySQL database and want to print the date as 5 Oct 2007 17:05. The MySQL format is 2007-10-05 17:05:00.
Now in normal PHP it would be:
echo date('j M Y H:i', strtotime($mysqlDate)); //$mysqlDate is the date from the database
Here is the same thing in Smarty:
{$mysqlDate|date_format:"%e %b %Y %H:%M"}
In Mysql it would look like this:
SELECT DATE_FORMAT(`date_column`, '%e %b %Y %H:%M') AS newDate FROM the_table WHERE ...;
I guess one could argue that Smarty should adapt to PHP. If you consider that PHP is developed in C, would it not make more sense for PHP to use C style specifiers?
Here is a list of specifiers for Smarty:
http://www.phpfreaks.com/smarty_manual/page/language.modifier.date.format.html
Here is PHP’s list:
http://www.php.net/manual/en/function.date.php
Here is for MySQL:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
PHP, Programming Stii
Oct
03
Python’s documentation is confusing and frustrating. As great as the language is, their documentation could be a lot better. Not the content of their documentation, but the navigability and structure thereof.
If you compare Python’s documentation to PHP’s documentation, you would see why. PHP got everything in one place. One book so to speak. Python got it all over the show in different “books”. There is nothing wrong with this apart for the fact if you go into the Language Reference book, go to a section in this book, there is no link back to other books like the Library Reference. A couple of links somewhere on every page linking to the other books would work wonders. The module reference is awesome, but as soon as you go into a module’s reference page, you need to click the modules button at the top or bottom to get back to a list of all the modules. Why not simply have a list of modules down the side as a menu, much the same as that PHP does with its function references?
One really frustrating thing is there is no search on hand in the Python docs. PHP got a search box on every documentation page and nothing is faster than simply entering the search phrase if you need to check something on a specific function. Python’s search is powered by Google and very accurate and effective, but you have to go to a separate search page, which is like the rest of the navigation in the documentation, pretty unreachable once you’ve left the page.
Another “nice-to-have” which PHP does and that would be really handy in Python’s documentation is the user examples/comments. PHP’s documentation is not at all as complete as Python’s is, but that simple little feature that allows users to leave some examples or comments makes the world of difference.
This might be a bit pedantic and petty, but it would just make lives simpler if it was done better! Yes, there are workarounds for almost all of the above issues by opening arrays of new browser tabs/windows. For now, workarounds will have to do.
Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
PHP, Python Stii
Sep
20
Lets face it, there is certainly no shortage of editors and IDE’s out there today. One would think that command line editors like VI or VI iMproved, Nano, Emacs, etc. should die a really sudden, quick and painless death. Yet, you’d be sorely mistaken. These editors will stay the editors of choice on any *nix server that does not have a GUI installed. Plus what could be simpler than typing
sudo vi /etc/apache2/httpd.conf
to edit system configuration files?
Me and Charl also had a discussion about how hardcore old school geeks swear by it, doing all their programming in these editors. You’d wonder why. I sure did, so I whipped out vimtutor and sharpened the ol’ VIM skills. Now, working with it more and more and getting used to all the shortcuts and keystrokes, it is becoming clearer by the day why it remains the editor of choice for hardcore geeks. It makes it fast! I’m by no means a hardcore geek, but I do see why they like it.
Admittedly, it is quite a learning curve, but once you are used to the commands its much quicker raking in those keystrokes than it is clicking all over some editor to get your rocks on!
It has got really good syntax highlighting and just in case you are afraid you will sorely miss your IDE’s tabbed open document interface you can easily open as much terminal windows as needed to suit your needs. All in all, I think I’ll stay a little longer with VIM than what I expected…
Multiple windows with VIM

PHP syntax highlighting

Python syntax highlighting

Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
Kick-ass Tools, PHP, Programming, Python Stii
Apr
02
While Ruby on Rails and Python is way ahead in the parsing of HTML for microformats, PHP is trailing behind it seems. We have found a couple of classes that can do it, but none without some quirks.
The one option was hKit which is a PHP 5.x parser. The problem with this parser is that it only works on PHP 5.x since it requires the SimpleXML PHP extension. Our problem is that not all PHP developed sites are running on PHP 5.x. That is not an ideal solution then.
The other option we found was the Microformats Parser over at PHPClasses developed by Ve Bailovity. This is an award winning class and is dependant of the xArray class also found there and also developed by Ve. This opion is much better since it works on PHP 4.3 or better and requires the DOM XML extension. The issue is that it does not work “out-of-the-box”. It worked beautifully when tested with the HTML file that accompanies the classes, but as soon as I tried it with some “real-world” microformat implementations (tried on http://corkd.com and http://www.thinkvitamin.com/), it failed. Sad, since it is a beautiful script!
So what do we need? We need a class that works on ALL versions of PHP and that can process ANY form of HTML or XHTML. We all know not everyone cares much for their mark-up and therefor you will find all kinds of funny and weird issues, so it must be extremely forgiving when parsing the HTML document. It must also have some proper security and anti-spam checks built in, since this could end up being a SPAMMERS dream if not properly protected! We’re on it.
We have started to develop our own parser class for kupa, aptly named kupaParser and will release it under the GPL for anyone who would like to take advantage of microformats on PHP. Will keep you updated on the progress as we go along.



technorati tags:kupa, microformats, semanticweb, php
Blogged with Flock
Sharing is caring:These icons link to social bookmarking sites where readers can share and discover new web pages.
Kick-ass Tools, Open Source, PHP, Semantic Web, Software Development Stii
Recent Posts
My Posse
Filed in
Past Stuff
Meta Stuff