 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Jaisen Methai's Blog: Stop including class files and use __autoload() instead
by Chris Cornutt July 02, 2008 @ 11:11:50
On his blog, Jaisen Methai posted about a very handy feature of PHP5 - the __autoload function.
It's one of the gems in PHP that I find to be relatively under used. It's common for PHP applications to break out classes into their own files. This becomes cumbersome when working on large projects as you wind up with numerous include/require calls for any given page.
He shows an example of its usage (loading class files dynamically from a specified directory) and mentions how it can help to make your code a lot less ugly. Comments on the post range in topics - from comparing it to include paths, how the idea was borrowed from PEAR, and how proper naming conventions can make its use even easier.
voice your opinion now!
include class file autoload php5 tip
DevShed: Improving Exception Throwing when Auto Loading Classes in PHP 5
by Chris Cornutt December 18, 2007 @ 12:56:00
DevShed continues their looks at autoloading classes in PHP5 today with this new tutorial focusing on catching and handling any errors that might be thrown in the process.
This is the third article in the series on how to auto load classes in PHP 5. This article will demonstrate how to trigger exceptions in a way that can be caught by the corresponding "catch()" block. [...] Nonetheless, this issue can be fixed with relative ease. That will be the goal of this third article of the series. So if you're interested in learning how this solution will be implemented, don't waste any more time and start reading now!
They start by looking at throwing an exception with the __autoload so that the script can catch it. The go back to mention the method they'd talked about before - using just eval() - and then show the "new and improved" method of handling the result with a try/catch block instead of a direct output.
voice your opinion now!
php5 exception throw autoload class tutorial php5 exception throw autoload class tutorial
DevShed: Throwing Basic Exceptions When Auto Loading Classes in PHP 5
by Chris Cornutt December 12, 2007 @ 07:55:00
DevShed has posted the second part of their series looking at handling exceptions in a PHP5 application today. This one focuses on capturing the errors thrown when your script autoloads classes.
Bearing in mind this intrinsic limitation exposed by the "__autoload()" magic function when it comes to triggering exceptions at runtime, in this second article of the series I'm going to show you some basic workarounds that you can implement to provide this function with the ability to throw exceptions when a determined source class fails to be included.
They give two examples of the use of the __autoload function as well as a method to capture the fatal errors that it might throw (using an eval inside of it to check on the class).
voice your opinion now!
php5 autoload throw exception class php5 autoload throw exception class
Joakim Nygard's Blog: Optimizing PHP Through Habits
by Chris Cornutt April 25, 2007 @ 10:39:00
Spurred on by some previous benchmarks [pdf] from Ilia Alshanetsky, Joakim Nygard decided to run some his own benchmarks on the same sort of functionality.
There are numerous discussions in the blogosphere about whether to use echo versus print, if for() is faster than while(), etc. and though the gains are usually very small, I decided to add my thoughts to the debate. I found an article on optimization through coding habits in Ilia Alshanetsky's zend performance slides.
According to his results:
- Calling require_once() 10000 times in a for() loop with an empty file is 4x faster.
- With a simply autoload requiring a class and 10000 loops of new Foo() versus require_once('foo.php'); new Foo() shows that __autoload() is ~3.7 times faster.
- If a class method can be static, declare it static. Speed improvement is by a factor of 4.
- Avoid function calls within for() loop control blocks
- Always, always quote array keys.
- Get rid of 'harmless' error messages - they take time to generate and output.
I am not out to prove Ilia wrong - he knows PHP better than most - and for all I know, they could have optimized those very functions in PHP 5.2. [...] It would appear that there are improvements, albeit small, to achieve from minimal effort. Plus I was surprised by the discrepancies I found compared to Ilia's recommendations.
voice your opinion now!
optimize coding habit benchmark requireonce autoload loop optimize coding habit benchmark requireonce autoload loop
|
Community Events
Don't see your event here? Let us know!
|