I have main PHP file, and dozen of less files, something like modules, they work this way (imaginary file):

if ($_GET["action"] == "login") {
    require_once("login.php");
    return login_something($_GET["credit"]);
}

That require_once(); and return function(data); recur a lot in my code. It looks kinda nasty, is there no way to make this action more direct? As in (imaginary code): return require_once("login.php"):login_something($_GET["credit"]);? Like a one-liner.

Please, don't ask why I seperate functionality of codes in different files, it's easier for me to maintain by just looking at it's name and search through robust code. Instead of scrolling 5000 lines PHP file.

From all the snippets, it looks like they just load the file by it's name:

spl_autoload_register(function ($class) {
    @require_once('lib/type/' . $class . '.php');   
    @require_once('lib/size/' . $class . '.php');
});

And:

spl_autoload_register(function ($class_name) {
    include $class_name . '.php';
});

And every-others-look-a-like. I know the name of the file, I could also do:

$class_name = "login.php";    
include $class_name;

I don't see any advantages. Well, I guess I could directly call Class->Function(). But, I'm not doing OOP, I'm making procedural code. My application is too simple to build entire classes upon that.

Look specifically at:

It was suggested in that discussion: he's using namespaces to autoload function helper files, basically you create an helper file for each custom function and then you use a class to load it. Live example here, with few fixes:

In practice, after spl_autoload_register(); you declare which namespaces you want to use and then you can start calling your functions:

<?php

    spl_autoload_register();

    use php\helpers\Fn;
    echo Fn::custom_function('Hello');

A part that loader class you don't need to do OOP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.