autoload in classes using mvc [closed]

-2

You can do this in php

use COntroller;
class Bootstrapp(){

public static function logar(){
$controller=new \Controller\$controlador();
}

I've tried here and the sintaxi error

here below the code

if (is_readable($caminho)) {

            require $caminho;
            $controlador = ucfirst($controller);
           ( $controller = new \controllers\$controlador;) aqui onde da erro

            if (is_callable(array($controller, $metodo))) {

                $metodo = $pedido->getMetodo();
            } else {
                $metodo = "index";
            }

            if (isset($parametro)) {
                call_user_func_array(array($controller, $metodo), $parametro);
            } else {
                call_user_func(array($controller, $metodo));
            }
        } else {
            header("Location:" . URL . "error");
        }
    
asked by anonymous 30.09.2014 / 15:54

3 answers

2

Well, I use this function. It is used as follows:

The function below goes in the autoload.php file

function funcaoCarregadora($ClassName){
    $pasta = ultimaPalavra($ClassName);
    include_once($pasta."/".$ClassName.".php");

}

function ultimaPalavra($string){
    $tmp = '';
    $char = '';
    for($i = strlen($string)-1; $i >= 0 ; --$i){
        $char = $string[$i];
        $tmp .= $char;
        if(ctype_upper($char)){
                break;
        }
    }

    return strrev($tmp);
}

Then in the config.php file I give an autoload.php include

include_once ('autoload.php');

config.php, has the function of security and redirection.

And on every page I give an include of config.php

include_once ('config.php');

and below that includ I instantiate my classes

ready.

I hope I have helped!

Note: my classes are named as follows:

File

MyClasseDao.php

class

MyClasseDao MyClasseModel MyClasseController

and View can be any name ...

    
12.04.2015 / 00:55
1

I revamped Google and found the solution

 if (is_readable($caminho)) {

            require $caminho;
            $controlador = ucfirst($controller);
//Adicionei a linha abaixo
      $namespace = "\" . "controllers" . "\" . $controlador; 
            $controller = new $namespace;

            if (is_callable(array($controller, $metodo))) {

                $metodo = $pedido->getMetodo();
            } else {
                $metodo = "index";
            }

            if (isset($parametro)) {
                call_user_func_array(array($controller, $metodo), $parametro);
            } else {
                call_user_func(array($controller, $metodo));
            }
        } else {
            header("Location:" . URL . "error");
        }
    
30.09.2014 / 16:16
1

Your question and answer does not seem to compose the OOP code, if you can broaden the example I can give a wider answer, but for now, I think this example can be useful.

You can create a simple autoload. I used stream_resolve_include_path to check if the file exists before importing.

spl_autoload_register( function( $file )
{
    if( stream_resolve_include_path( "{$file}.php" ) === false )
    throw new Exceptions( "O arquivo '{$file}' não existe." );

    include "{$file}.php";
});
    
30.09.2014 / 22:22