PHP auto class inclusion
26th June 2002
When developing PHP applications, I usually have a classes directory somewhere in which I keep all of my PHP classes ready for inclusion. I name the class files ClassName.class.php
. Normally I have a common.inc.php
file that is included in all of the scripts in my application and requires the classes needed by the application, but today I wrote a few lines of code that saves me from having to alter that file every time I write a new class:
<php $classesDirectory = 'classes/'; $classesExtension = '.class.php'; // require_once all classes in that directory $d = dir($classesDirectory); while (false !== ($entry = $d->read())) { // Check extension if (substr($entry, -(strlen($classesExtension))) == $classesExtension) { require_once($classesDirectory.$entry); } } $d->close(); // print_r(get_declared_classes()); ?>
It’s nothing revolutionary, but it’s already saving me development time by allowing me to save a file straight in to the directory and instantly have the new class available to all scripts in the application.
More recent articles
- Datasette Enrichments: a new plugin framework for augmenting your data - 1st December 2023
- llamafile is the new best way to run a LLM on your own computer - 29th November 2023
- Prompt injection explained, November 2023 edition - 27th November 2023
- I'm on the Newsroom Robots podcast, with thoughts on the OpenAI board - 25th November 2023
- Weeknotes: DevDay, GitHub Universe, OpenAI chaos - 22nd November 2023
- Deciphering clues in a news article to understand how it was reported - 22nd November 2023
- Exploring GPTs: ChatGPT in a trench coat? - 15th November 2023
- Financial sustainability for open source projects at GitHub Universe - 10th November 2023
- ospeak: a CLI tool for speaking text in the terminal via OpenAI - 7th November 2023
- DALL-E 3, GPT4All, PMTiles, sqlite-migrate, datasette-edit-schema - 30th October 2023