I have to implement the Autoload class in my project with the spl_autoload_register function. As it did not work, try to try it in a simpler code. I have three folders in my mini project: One call Config, where is the class AUtoload.php
<?php namespace Config;
class Autoload {
public static function iniciar() {
spl_autoload_register(function($classPath)
{
$ruta = ROOT . $clase . ".php";
print $class;
echo '<p>' . $class . '</p>';
include_once($class);
});
}
}
?>
A Models folder where I have a class called Person that I just want to say hello
<?php namespace Modelos;
class Persona
{
public function hola()
{
echo 'La persona dice hola';
}
}
?>
And in the index I start the Autoload, I instancio a new person and I execute the hello method.
<?php
define('ROOT', __DIR__ . "\");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require('Config/Autoload.php');
Config\Autoload::iniciar();
$persona= new \Modelos\Persona();
$persona->hola();
?>
I miss this error:
C: \ wamp \ www \ test_autoload \ Models \ Persona.php (!) SCREAM: Error suppression ignored for (!) Fatal error: Class 'Models \ Person' not found in C: \ wamp \ www \ test_autoload \ index.php on line 12 Call Stack
What is the line where the new person instants. Take the class as Models \ Person, and not as the Person class. In other words, put the name of the folder or namespace inside the class name. Not looking for Person but "Models \ Person" as a class.
I do not understand how to solve it.