Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

At the moment I am auto loading my classes with:

spl_autoload_register(function ($class) {
    require_once 'classes/class.'. $class .'.php';
});

And instantiating the class with:

$backend = new backend();
$dashboard = new dashboard();
$article = new article();
$video = new video();
$theme = new theme();

These are in a config file required on every action page of my admin section.

backend is my main class holding database functions and other global functions.

The other classes all extends backend.

As I understand it, I could call backend functions through for example article, as it is an extension of the class.

Is this a good method for instantiating the classes, or is there a way I can call them automatically without having to write these in the config file at all?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Using spl_autoload_register() is a well known and recommended way of auto-loading classes.

And of course you can't call objects without having them instantiated first.

share|improve this answer
    
The way I'm doing it just looks so unprofessional, is there a cleaner way to instantiate? –  CodeX Aug 30 '14 at 20:59
1  
The only way to instantiate a class is like new ClassName();. "To create an instance of a class, the new keyword must be used." - PHP manual. –  Kid Diamond Aug 30 '14 at 21:03
    
Ok, thanks KD ill carry on as is :) –  CodeX Aug 30 '14 at 21:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.