Category Archives: autoload

PHP: Trouble with autoloading

I am creating a website and I am using Apache as my webserver.

I have created a website without the use of classes. All of my functions are located in one file and I just include this file on every page where I need to use some of the functions.

I am now wanting to swap to an OOP approach but I am having trouble understanding how to autoload my classes. I have been through a number of related pages, such as; PSR-4 Example, spl_autoload_register(), Related Question and I just can't seem to get my head around this.

So, as I am using Apache, the path for the root of my website is C:\Apache\htdocs.

My directories look as follows;

+ htdocs
    + Lib
        + Base
            - User.php
            - Device.php
            - Company.php
            - Database.php
        + Services
            - UserService.php
            - DeviceService.php
            - CompanyServer.php
        + Config
            - DbConfig.php

As an example so you can help me get my head around this, we will just talk about two classes DbConfig and Database.

DbConfig.php (with connection details omitted)

namespace Lib\Config;

class DbConfig
    protected $serverName;
    protected $userName;
    protected $password;
    protected $dbName;

   public function __construct()
        $this->serverName = 'not my server name';
        $this->userName = 'not my username';
        $this->passCode = 'not my password';
        $this->dbName = 'not my database';

Database.php (Very simple until I can actually use it - at which point I will add its functionality)

namespace Lib\Base;

use Lib\Config\DbConfig;

class Database extends DbConfig
    protected $connection;
    private $dataSet;
    private $sqlQuery;

    public function __construct()
        $this->connection = null;
        $this->dataSet = null;
        $this->sqlQuery = null;


  1. How do I load these classes, using some implementation of spl_autoload_register(), into another .php file so that I may use them to create objects? for example in a file named 'Testing.php'

  2. Previously I would parse a .ini file outside of the root of my folder to get the database connection details, should I do this with my new approach of having the connection details in the class DbConfig?

Who decides whether to display a resource or run the script when all request are routed through one file? [on hold]

There are several ways to be Rerouting all php requests through index.php or another specified PHP file. It has several benefits for increased customization, SEO, possibly security as you don't reveal too much of the system you're using...

Using this technique, what is best practice / ideal behavior for an OOP based framework to actually show resources, like images, stylesheets, etc. when needed?

So for example, obviously point all to scripts. But,, point to resources that need to be served with the appropriate header. Also I assume there is a third case, where you point to an error page, be it 404 or something else (some misconfiguration and the request couldn't display a proper result). And maybe you want to be extra thorough and allow for to be either treated as or redirect it there, to minimize the number of 404 errors.

So again, for one request you could do one of the three:

  • redirect or interpret wrong, but similar urls
  • show the respective script
  • load the resource
  • show an error page

I hope I am making sense so far.

Now of course you could make a big long if/else if statement, and by analyzing $_SERVER['REQUEST_URI'] for file extension, etc. you can determine which of the three options to take.

Is there a better, OOP oriented solution though?

I'm thinking there might be a request object, that dissects the URL, and the result of that gets used by the router or dispatcher... but who of those should make that decision? Router, Dispatcher, Request Object, Front Controller, or a totally new object?

While thinking about that, I assume that performance might be an issue - in which order you check things, etc. can effect how fast a resource loads. Especially when for example any php script needs to access the DB first, to even be validated (e.g. a CMS where the admin can set up in a db which pages are available and which aren't).

I don't know what keywords to search for and get more information on this subject. I found something about the php webserver, but it specifically said not to use this for production.

And yes, I have tried looking at existing frameworks and how they do it, but their solutions are always so convoluted and complicated right off the bat that I can't make sense out of it... sorry!