autoload with extended classes, inheritance

0

I have the following autoload

<?php

spl_autoload_register('_autoload');
define('DS','/');

function _autoload( $class ) {

    $class = ROOT  . str_replace("\",DS,$class) . '.php';

    if(!file_exists($class)){
        echo "Error al cargar la clase " . $class;
        die();
    }else{
        require_once($class);
    }
}

This works perfectly in classes without inheritance, the problem is when one class inherits another, the autoload loads the class mother / father and therefore does not load the class I want, here an example;

TaskController.php

use Models\Tasks\Task;

class TasksController extends Controller
{
    function index()
    {
        $tasks = new Task();
        $data['tasks'] = $tasks->showAllTasks();
        $data['cantidad_tareas'] = $tasks->tasks_number_by_status(0);
        $this->setData($data); //envia datos a la vista
        $this->render("index",'Index Page'); //Renderiza la vista con un titulo
    }
}

TaskModel.php

namespace Models\Tasks;

class Task extends Model
{
........

The next screen is a custom screen that I have for when errors occur within my framework, as you can see it says that the problem is in the class 'Model.php' but the class you want to load is 'TaskModel.php' ;

The idea is to create a autoload that is capable of loading all classes without any problem as well as we should optimize the performance since only the classes or files that we use will be included.

    
asked by Albert Hidalgo 29.10.2018 в 21:27
source

2 answers

0

The problem in itself is that it did not have the namespace declared in class Model ;

namespace Core; //Simplemente lo agregue y funciono perfecto

class Model extends DataBase
{
 //....
}

Not having this, the autoloader will try to load the parent / class with the namespace of the daughter class.

    
answered by 30.10.2018 / 02:22
source
1

Albert I have my class Autoloader written like that and I have no problem:

<?php

class Autoloader
{
    public function __construct()
    {
        spl_autoload_register(array($this, 'loader'));
    }
    private function loader($className) {

        $str_path=realpath(dirname(__FILE__));
        $filename = $str_path .'/'. str_replace('\', '/', $className) . ".class.php";

        include_once($filename);
    }
}
?>

Here are some things:

  • Use spl_autoload_register
  • In all my classes I use a file name convention . That is, all my class files are within the same %%_country folder and end with% co_of%

For example, I have these two classes:

class Misa extends Liturgia{

    private $id;
    //...

    public function __construct(){

    }
    //Métodos
}

Y:

class Liturgia extends UtilStrings
{
    private $intLiturgia;
    private $intTiempo;
    //...

    public function __construct(){

    }

    public function setTiempo($intTiempo){
                $this->intTiempo=$intTiempo;
    }

    //Más métodos
}

Now I use my autoloader simply like this:

require_once(CLASS_PATH."Autoloader.class.php");
$autoloader = new Autoloader();

$misa=new Misa();
#setTiempo y getTiempo son métodos de la clase padre Liturgia
$misa->setTiempo(5);
echo $misa->getTiempo();

#más aún, getCopyRight es un método de la clase UtilStrings, de la que extiende Liturgia
echo $misa->getCopyright(2013);

This code works without any problem.

    
answered by 30.10.2018 в 01:55