Home > database >  PHP undefined constants/functions when using spl_autoload_register() and namespacing
PHP undefined constants/functions when using spl_autoload_register() and namespacing

Time:01-26

Hi there,

Sorry if the title is rubbish,

I am new to learning PHP, and right now I am trying to make use of the spl_autoload_register() function to handle class includes,

I have made a little project to try and figure out what I am doing wrong, specifically why consts and functions can't be found,

My project folder is as follows:

Site

  • index.php (include config.php)
  • config.php (spl_autoload_register())
  • src

    • classes

      • ClassA.php
    • constants

      • CONSTANT_A.php
    • functions

      • functionA.php

Errors

The main error I keep coming across is:

Fatal error: Uncaught Error: Undefined constant "constants\A"

or

Fatal error: Uncaught Error: Call to undefined function functions\A()

The class is being pulled through just fine it's only the constant and function that are failing,

Page examples

<?php
// index.php
include "config.php";

use const \constants\A;
use function \functions\A;

$my_obj = new \classes\ClassA();

$my_const = \constants\A;

$my_function = \functions\A();
<?php
// config.php
function myAutoLoader($class)
{
    $extension = ".php";
    $path = "src/" . str_replace("\\", "/", $class) . $extension;
    if (!file_exists($path)) {
        return;
    }
    require_once $path;
}
spl_autoload_register("myAutoLoader");
<?php
// ClassA.php
namespace classes;
class ClassA
{
    public function __construct()
    {
        echo "Hello ... " . PHP_EOL;
    }
}
<?php
// CONSTANT_A.php
namespace constants;
const A = 1;
<?php
// functionA.php
namespace functions;
function A()
{
    echo "Hello ... " . PHP_EOL;
}

My thinking is it has something to do with the constant and function not being a part of a class so the spl_autoload_register() is not including the relevant files,

I have tested the above idea by removeing the spl_autoload_register() and just including the files like normal and that works but I thought the point was to not have to do that?

I have seen other answers around sort of the same problems with constants where people have suggested that you need to use define instead of const and include the namespace with it so:

namespace constants;

define("constants\A", 1);

But that still makes the same error,

Thank you for any response,

CodePudding user response:

The answer is very simple: PHP autoloading does not support functions and constants. As the manual says:

Any class-like construct may be autoloaded the same way. That includes classes, interfaces, traits, and enumerations.

There are some technical reasons why the feature can't be extended to functions and constants, but from a user's point of view, just know that it's not currently supported, and is unlikely to be supported any time soon.

So for now, you have two choices:

  • Group your functions and constants in files which you include manually in some general "startup" / "bootstrap" code. If you are using Composer (which I highly recommend), it can manage such files alongside your main autoloader, using the files key in composer.json.
  • Use class constants and static methods, instead of namespaced constants and functions. So instead of \constants\A; you would write \Constants::A; and instead of \functions\A(), you would write Functions::A(). Then the classes (\Constants and \Functions) will be autoloaded for you.
  •  Tags:  
  • php
  • Related