Problems with autoload in php

0

Colleagues

I have the following question, it turns out that I am working with mvc without a framework, and I have problems with autoload gives me the following error

  

Fatal error: Uncaught Error: Class 'Model \ ProjectModel' not found in   C: \ xampp \ htdocs \ Wallpapers2 \ index.php: 42 Stack trace: # 0 {main} thrown in   C: \ xampp \ htdocs \ Wallpapers2 \ index.php on line 42

straight to the point,

this is the code of my autoload

<?php namespace config;
 class Autoload{
public static function run(){
    spl_autoload_register(function($class){
        $ruta = str_replace("\", "/", $class).".php";
        if(is_readable($ruta)){
            include_once $ruta;
        }
        //print $ruta;
    });
}
}

this is the code of my index:

  require_once "config/Autoload.php";
   config\Autoload::run();
   new Model\ProyectoModelo();

and this is the code of my model

<?php namespace Model;
class ProyectoModelo
{
private $IdVicarias;

private $NombreProyecto;

private $SolicitudAporte;

private $AnoProyecto;

private $PersonaResponsable;

private $RbtEntregaCuota;

private $TotalRendiciones;

private $SaldoxRendir;

private $SaldoRendido;

private $porcCuotasRecibidas;

private $con;

public function __Construct()
{
    $this->con = new Conectar();
}
}

If you could help me I would be very grateful

    
asked by Pablo Moraga 19.10.2017 в 20:09
source

2 answers

1

How did you order the files? Maybe you do not have the model inside the Model folder, it is a headache and more when your work is enlarged, I recommend using composer, it generates a much smarter autolaod than the one you are wanting to use (Many use it). Using composer helps you for this and much more things such as using third-party code and adapt it to your project, otherwise you will have to download the files manually and then place them in the folders that your current autoload is reading, that is a headache , with composer you establish a folder where your classes will be and it calls them when you need them and if you have other folders inside of it, you will not have problems, nor will you have problems with the namespaces.

My recommendation is that, how to do it?

Requirements (have installed):

  • Composer
  • GitBash
  • Once both are installed, you do the following:

  • With the Git Bash console you go to your project directory
  • You create a file called composer.json
  • With the console you execute the following command composer update
  • This will create a folder called "vendor", it will keep all libraries, jobs or third-party classes that you can then use.

    With this you have already installed composer but still need to configure the folder where your classes are.

    For that we do the following:

  • In your index.php you have to include the Composer autolaod: require_once 'vendor/autoload.php';
  • You open the file that you created (composer.json) and modify it to your liking, here I leave you as I have it in one of my projects:
  • Inside the folder called "app" I have all my classes, you can have it wherever you want as long as you clarify it in the composer.json

    Plus!

    If you notice you have a field called "files" which directs you to an .php file in that file I have functions that can be executed from any class without having to instantiate anything, they are loose functions that I can call them from where I want.

    Once you have your classes in the "app" folder you can instantiate it from the index.php with the namespace you are using.

    NOTE Every time you create a new class you have to update the autoload and you do it in the following way:

    With the console you go to the root of your project and with the GitBash console you execute the following command: composer dump-autoload

    Ready!

    Other information

    I have my folder organized like this:

    • App
    • --- Drivers
    • --- Models

    And inside each folder I have other classes, you organize it as you want, but you have to have a "mother" folder, so to speak, composer maps the entire folder with their respective namespaces and that's it!

    I hope you serve as ami, what you are doing gave me several headaches and more when the project is enlarged and are more classes and others. Greetings

        
    answered by 19.10.2017 в 20:57
    0

    It is possible that the generated route is not considering the DirectoryRoot route.

    use dirname(__FILE__) to consider it and if it is in a subfolder the autoload comes out of it with '../'

    spl_autoload_register (function ($ class) {     $ class = ltrim ($ class, '\');     $ path = dirname ( FILE ). '/ .. /'. str_replace ("\", DIRECTORY_SEPARATOR, $ class). ". php";     if (is_readable ($ path)) {         include_once $ route;     }     // print $ path; });

        
    answered by 20.10.2017 в 00:01