Autoload settings

3

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.

    
asked by merisavino 07.11.2016 в 20:56
source

3 answers

1

You should put it this way:

$persona= new \Modelos\Persona();

because in this way you are referring to the namespace in which your class really is. If you do not put the backslash just before Modelos then you are taking it as if the class were in the current namespace.

On the other hand, the Config\Autoload if you are taking it because you have imported the file directly into your index.

This explanation can be read in the PHP documentation here and here .

EDIT: The problem now is that you can not find the route. This is because you are changing the backslashes of the route by bars like this: / in this line:

$ruta = ROOT . str_replace("\", "/", $clase)  . ".php";

Do not do the replace because otherwise the route will not take it since it will have inverted bars \ and normal bars / .

    
answered by 07.11.2016 в 21:04
1

You forget to use / implement the class:

use Modelos\Persona; //<-- !!

$persona= new Persona(); // <-- Sin el namespace
$persona->hola();

EDIT:

To be more specific:

Suppose you have your routes this way:

root
|
|__ Modelos // Carpeta Modelos
|      |
|      Persona.php // Tu clase Persona 
|
autoload.php
index.php

autoload.php :

<?php

spl_autoload_register(function($className) {

    $path = __DIR__.'/'.str_replace('\', '/', $className).'.php';

    // Por si acaso, comprueba si existe ese archivo
    if (file_exists($path)) {
        require $path;
    }    
});

index.php

<?php

require __DIR__.'/autoload.php';

use Modelos\Persona; // Usar la clase Persona

$persona = new Persona();
$persona->hola();
    
answered by 08.11.2016 в 01:32
1

A thousand thanks to all, the error was in the definition of Root

define('ROOT', __DIR__ );

Then, the Autoload function would be like this:

spl_autoload_register(function($classPath)
        {
            $class = ROOT . "/" . str_replace("\", "/", $classPath)  .          ".php";
            //echo '<p>' . $class  . '</p>';
            include_once($class);
        });

Thank you very much everyone!

    
answered by 08.11.2016 в 06:19