PHP Setup Object-oriented classes correctly?

I've been told I have to create a config.php file that will hold all of the classes.

For example let's say I have 3 classes.

Class 1: House

Class 2: myCar

Class 3: Road

And in config.php I call them:

$house = new house();

$mycar = new myCar();

$road = new Road();

and then I include that config.php file in every page, for example index.php.

But now, I want to extend class Road.

I can't include config.php in that class? Or include the other' class file in it while it is already included in config.php.

Is this the wrong way of setup for classes?

How can I extend classes without having errors.

Do I have to create a new object everytime?

Example:

Instead of including a config.php Ill do this:

/**
 * Index.php
 **/
include("class/house.class.php");

$class = new House();

echo $class->echoMe("hey");

And in every file I will do the same, by creating new objects, so then I can extend some specific classes when needed?

Answers


function autoload($class) {
    if (is_file($file = 'www/content/includes/class/'.$class.'.php')) {
        require_once($file);
    }
}

spl_autoload_register('autoload');

Include the following code in your config.php file. Make sure that you include the config file in every file where you want to load your classes.

Make sure that the file has the same name as the class your calling. For an example

$house = new House();

would require the class filename to be House.php

Start each class file with

<?
    class House {

        function something() {

        }

    }

Need Your Help

How to make the jQuery valid function work reliably on IE?

javascript jquery internet-explorer validation

I have a problem on jQuery valid function. When on IE, it doesn't work, the valid always return true. I used this code: client side validation with dynamically added field

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.