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.