TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-06-2007, 12:07 AM   #1 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default Tip: Autoloading class files

You can autoload class files using the __autoload() function. Simply define the function with a single argument (this will be the name of the class) .

Code:
function __autoload($szClassName)
{
		$szFilePath = "lib/$szClassName.php";
		
		if (file_exists($szFilePath))
		{
			include_once($szFilePath);
		}
}
Now if you try to instantiate a class that is unknown, PHP will first try to load the class file via the code we setup in the __autoload funciton before throwing a fatal error.

Last edited by Karl : 09-06-2007 at 01:44 PM. Reason: I made a boo boo :(
Karl is offline  
Reply With Quote
Old 09-06-2007, 01:37 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Your code snippet will not work as-is. You assign $szFileName but then use $szFilePath in the file_exists and include_once calls.
Salathe is offline  
Reply With Quote
Old 09-06-2007, 01:43 PM   #3 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

ah yes lol, sorry. Wrote it from the top of my head, guess I should have checked it first. All fixed up now :)
Karl is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:48 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design