A simple PHP 5 __autoload() example
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.
One Comment to "A simple PHP 5 __autoload() example"
Spit it out!
PHP Stii
Recent Posts
- Afrigator Beta2.0 - Content and usability
- Afrigator Beta2.0 - Custom feature
- Afrigator Beta 2 - Tons of new stuff…
- Spammers offer nothing for R1 plus postage and packaging
- Bloody Stupid Johnson
- A really great music video!
- Facebook chat is not about replacing your IM, its about connecting with people in realtime.
- Vernon Koekemoer got R20 000 for his Vodacom ad. This is for you Wezzo!
- On Earth Day, I found an Arrowhead Flatworm!
- Follow your friends’ submissions on Muti
My Posse
- Jayx’s bloggy
- Gogo’s blog
- Go2 South Africa
- Stumble Upon
- Dave Duarte
- Wikipedia
- zlythern
- E Active
- Max Kaizen
- Tresblue
- Mike Stopforth
- RafiQ
- Muti.co.za
- Employmint
- Danette’s Bloggy!
- Thinking Machine
- White African
- kiefpiet.co.za
- Skuff’s World
- Top SA Blogs by muti.co.za
- Goozeberry
- Crossloop blog
- Crossloop
- Aquila Online
- Charl van Niekerk
- Derek Allard
- Code Igniter
- Carls
- Justin Hartman
- blik.co.za
- Stefano Sessa
- Uno de Waal
- Amplitude!
- bLaugh
- Tyler Reed
- Chris Rawlinson
- Stormhoek!
- 3am
- Mike Solomon
- Mobile Q and A
- Eric Edelstein
- Marc Forrest
- Imel Rautenbach
- Absolutewillie
- Vincent Maher
- Colin Daniels
- Groogle!
- Chilibean
- Paul Jacobson
- Ayelet
- Python Guru Neil
- Rails Guru Nic
- Beverley Merriman
- Miguel
- Nic Harrywhatshisname
- Chris iMod
- Geekrebel!
- Steven McD
- Belinda sweetheart!
- Henre Rossouw
- JPGeek
- Adventurous Mama!
- Foxinni
- Adii
- Charl Norman
- Bandwidthblog
- Jason Bagley
- Simon Botes
Filed in
- Afrigator (23)
- ajax (9)
- API (2)
- Apple stuff (10)
- Blogging (23)
- browsers (5)
- Business (27)
- Code Igniter (8)
- firefox (8)
- flock (14)
- Funnies (72)
- GeekDinner! (18)
- General and sometimes Rants (45)
- Go2SA (2)
- ideas 2.0 (13)
- javascript (12)
- Kick-ass Tools (30)
- Linux (4)
- Marketing (25)
- moo.ajax (4)
- mootools (6)
- Open Source (10)
- Programming (32)
- C# (1)
- PHP (13)
- Python (9)
- Ruby (on Rails) (9)
- RSS (5)
- Semantic Web (30)
- Social Web (57)
- Software Development (15)
- South Africa (31)
- Tagging (6)
- Techie stuff (22)
- Tshirts (2)
- Tutorials (42)
- Blogging (17)
- Flocking (6)
- muti.co.za (13)
- Web 2.0 (71)
- web development (20)
Past Stuff
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006















[...] some inside info about their main news site’s redevelopment. Going from ASP to PHP and going with a custom developed system rather than an Open Source [...]