Jack hardcastle
Jack hardcastle

Reputation: 2875

PHP Auto-execute script upon any page load

My Index.php

namespace Toplist;

require_once "Helper/Functions.php";

use Toplist\Helper\Functions;
use Toplist\Controller\IndexController;
use Toplist\Model\IndexModel;
use Toplist\View\IndexView;

My Functions.php

function __construct() {
    spl_autoload_register('self::l');
}

public static function l($c) {
    $n = $c . '.php';
    $f = preg_replace('/Toplist\\\\/', '', $n);
    $f = preg_replace('/\\\\/', '/', $f);
    if(!file_exists($f)) {
        return false;
    }

    require_once $f;
}

This works fine, autoloading is set up correctly - however, is there any php configuration I can do to automatically run the require_once "Helper/Functions.php" without having to actually declare that? I hate how require_once defeats the point of autoloading and the thought of having to declare it at the top of every script is upsetting me.

I know it seems trivial, and may not make much sense, but it's annoying me and I thought there might be a slight fix-around than using require/require_once or include for this, maybe something in the php.ini?

Thanks everyone!

Upvotes: 1

Views: 207

Answers (1)

symcbean
symcbean

Reputation: 48357

Yes, specify an auto-prepend in the php.ini file.

Upvotes: 2

Related Questions