Class Inheritance

I am doing a lot of reading these days on OOP and am doing some testing with classes and Inheritance. I downloaded an example and am quite confused about the way they used class inheritance. They have different classes all as separate documents. For exemple they have a file called cBasic.php and cLogin.php.


class cLogin {


class cBasic extends cLogin {

On index.php they include a config file

require "/includes/general.php";

This file holds the following function, which I don’t understand:

// Autoloader classes
function __autoload($sClassName)

This obviously the way they include the classes but am not sure how to implement it in future projects, Can someone tell me what this function is doing.

Thank you in advance

First, I’m surprised that require_once(“/includes/classes/{$sClassName}.class.php”) works with a file named cBasic.php for I would have thought it would have to be cBasic.class.php in order for it to work.

Second, I was told it was best to use spl_autoload_register() over __autoload() and this is what is in my file.

// Autoload classes from "classes" directory:
function class_loader($class) {
    require("lib/classes/" . $class . ".php");
// preferable over __autoload():

All this does is automatically puts the the classes on a stack and as far as I can tell it has nothing to do with inheritance for that is why classes are written. However, a more egghead probably can give you a better answer in that regard.

Many developers writing object-oriented applications create one PHP source file per class definition.

You may define an __autoload() function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

As the slowest part of any script is file system access, sticking each class in a separate file is a great way to slow down your script unless you are on a VPS or dedicated server and can use an opcode cache/accelerator.

Hmmm. Can you say FUD?

Thank you all for the replies. Very helpful

Your absolutely right Strider64. That was a typo.

I have one last question about this spl_autoload_register(). Normaly you would create a new object

$c = new cBasic();

Do i still need to do that on each page where i need the different classes?

I am not sure of this, but even if it slows down your script you still improve the architecture design of your script/software. For most programmers, they’d rather take this performance hit than simply writing multiple classes in the same file. As far as I know, every popular PHP framework does this, I’ve never seen one exception. Theres gotta be a reason for that, and I am sure you know that too.

Sure, I have seen included files with only one line of code in it. Wordpress does that. Joomla has includes with only a few lines of code in them. That is not efficient. You will find plenty of complaints about performance problems with these scripts. Compiled programs may get away with it but there is a performance hit with interpreted languages. And with a decent IDE like Netbeans, you can easily navigate between different classes within the same file with a simple click of a mouse. Assume a heavily loaded shared server where files are not cached in RAM for long (if at all). At an average seek time of 6 ms, it will take 6 ms x 80 files = 480 ms just to seek out the files containing the code which must then be compiled for Wordpress. That’s nearly half a second before any real work is done. File access will always be the slowest part of executing a program next to database queries. Might as well try to limit this performance killer as much as possible.