Published 2005-03-29 13:32:38

I had the pleasure (pun intended) of installing a small framework of code today, which broke at least half of the rules I've been building up for the projects I've been working on. The code illustrated very clearly why explicitly typing require_once is not only good idea, it can make the difference between clear readable code, and poor magic.

The error "Fatal error: Call to undefined function: somefunction_xyz() in..." appears after installing the code. Looking at the file, it only contains one line.. somefunction_xyz()!

The framework is supposed to have loaded this file, but as it's not set up correctly, and therefore, it didnt happen. To me this assumption that the file is loaded is flawed to begin with. Ignoring the issue that the framework relies on function libraries, the other fatal flaw is that Frameworks should rarely load more that one 'action' file, which in turn should be reasonably self explainitory where it is getting things from.

The missing require_once makes the code very difficult to follow without inside knowledge (or heavy use of grep) of how the framework may be working, and very little is given away as clues to what should have happened prior to this error occuring.

I guess this harks back to the idea that __autoload() will encourage people to write more code that is less self documenting, almost all languages C#, Java, Python... usually have a list at the top of the page, indicating what they 'import' or 'use' to achieve the aim of the program, PHP uses the require_once to document the source of your libraries. It helps others read your code, and in PHP can also be placed close to the place you actually use the library method. Alot of these language have ways around ending up with this large import list, and often some import's implicitly load others, but in making code readable it's often worth duplicating these, just to ensure that it's readable.

So from the trenches here, please try and make your code readable, other people have to install, set it up, and as quickly as possible understand what you intended to do....

Mentioned By: : Jeff Moore’s Blog » Blog Archive » Documentation versus Productivity? ( referals) : php | architect - The PHP Magazine for PHP Professionals ( referals) : april ( referals) : PHP News, Views, and Community... ( referals) : march ( referals) : Jeff Moore’s Blog ( referals) : Planet PHP ( referals) : december ( referals) : php require_once ( referals) : Re: [PHP-DEV] __autoload() enhancement ( referals) : Jeff Moore’s Blog » PHP ( referals) : difference between require and require_once in php ( referals) : Professional PHP » 2005 » April ( referals) : require_once ( referals) : difference between require and require_once ( referals) : Midgard Project - Open Source Content Management System (CMS) ( referals) : require_once is part of your documentation. ( referals) : Require once as Documentation - php | architect — The PHP Magazine for PHP Professionals ( referals) : Artima Developer Buzz ( referals) : php require once ( referals)


or you just ..
find the definition of the __autoload() method once. There you will be able to find the class to file naming rules and instead of grepping for require_once all the time you know the single location. Of course when you mix and match require_once with __autoload is does get nasty and confusing.

idiots will always find ways to make their code unreadable.
#0 - Lukas ( Link) on 2005-03-29 15:58:14 Delete Comment
Hi Alan - as you're working on frameworks, would you be interested in 'auditing' the Seagull framework? Lot of (probably incorrect) use of DB_DataObject and Flexy, we're always seeking to improve/refine the design, all feedback welcome :-)
#1 - Demian Turner ( Link) on 2005-03-29 21:35:48 Delete Comment

Add Your Comment

Follow us on