There is much debate about the __autoload function introduced in PHP5. I personally love it as all those requires, includes etc. made programs quite messy, and frankly annoy me if you create loads of pages that keep on requiring the same classes.
The one fault __autoload has though is that Exceptions cannot be thrown, and that to me would give potential for some great degradeablity.
Thankfully, there are a few ways of hacking the autoload function so that it does throw an Exception. The most common way is to create a class with the class name the autoload function is looking for using the eval() function, and then throw an exception once autoload is happy.
This has been tested on CentOS but should work on most distros:
Memcache is a caching mechanism for PHP which speeds up access to data by storing it in the RAM. Memcache runs as a server in a similar fashion to a database, except that data is not persistent in the long term. The great thing about Memcache is you don’t need to recompile php. Here’s some instructions on how to install and set it up.
Very simple bit of PHP to add to the top of your script if you want to password protect something. I wouldn’t reccommend this for larger bits of software, but if you have a simple script or page that you don’t want people to access, try the following:
Late last year, Google made a switch from the old YouTube API and integrated it into it’s GData API. While an understandable move, it does somewhat annoy me that the old REST method is being depreciated in August 2008 because I don’t particularly feel like updating my code 🙂
Alas, it will happen soon, and so I decided to upgrade some pages that used it. As I don’t heavily rely on the video services, and just use it in it’s simplest form for displaying a list of Videos, I made something quick, rather than install the Zend GData Library Google recommends.
Since GData outputs RSS, I used MagpieRSS to painlessly port my old code and have an updated code up and running. Here’s two scripts that may help with your changeover:
I needed to grab a couple of new blog entries on a website’s wordpress blog area, and put it on a non-wordpress home page. I ended up making a PHP 5 class that did the job for me. Very simple, no fancy stuff.
I then needed to get a load of youtube video posts on the same site from a specific account and had to make a list of that, with thumbnails etc. . I ended up tweaking the first class ever so slightly and lucky me, I had an easy feed reader.
The DOMDocument is a convenient way of manipulating an XML file. While this is for PHP, I’ve come across some great parsers for use on a business iphone using lib2xml but we leave that for another day. One issue I ran into was the fact that when loading an XML file, DOMDocument treats the tabs and spaces which make the XML readable as empty text nodes. This presents a problem when you try and traverse the DOM by using attributes like firstChild and nextChild. For example:
Here is the XML file, “example.xml”:
I was looking around for a simple function or something that calculates the age of a person with a given date. I figured there must be something that did this natively in PHP, but to my surprise…. no.
Further looking around found some overly complicated (not “complicated” but just way too long for what they do) scripts to calculate an age. In the spirit of easy minimal code, I did it in two lines :
$ageTimestamp = time() - strtotime($date);
$age = floor($ageTimestamp/60/60/24/365);
So…. quick explanation for those that are learning the ropes ….
The main reason for this blog here is to share with others the problems I’ve encountered and solved in the hope of saving someone banging their heads on the wall trying to solve issues that are sometimes trivial but waste stupid amounts of time.
A problem I recently encountered is that after doing some PHP magic and then redirecting a user to another page, the following page loses all the session information set in the previous page. For hours I was there trying to figure out why this was happening, and hopelessly trawling the net for answers!