0
It is a scheme that I am inventing since in this project I do not go by framework for being very simple,
Working with MVC in the Framework (Codeigniter),
But I don’t have a separate Model class with loading (only as a file), it’s always an extension of Controller, so I’ll create my controller, give them a name in the boot config and I want the load to load all of these that are in the array, together with the model of each.
What is missing is that it automatically loads the name of the $variable being the name of the Controller in minuscule letter, and the new = Class; instead of the word "Class" I want the name of the file that is the same name as the Controller class.
In short, auto load, but already give the new command to initialize the class, it is giving error.
Code:
<?php
/* ------------------------------------------------------ *
* Configuração do php
* ------------------------------------------------------ */
ini_set('display_errors', 1);
/* ------------------------------------------------------ *
* Configuração inicial do Sistema
* ------------------------------------------------------ */
$base_url = 'http://localhost:8081/bitbucket/16.4.2/';
$index_page = 'index.php';
$charset = 'ISO-8859-1';
$autoload = 1;
/* ------------------------------------------------------ *
* Configuração do autoload
* ------------------------------------------------------ */
$autoload['Controller'] = array(
"Cliente",
"Administrador",
"Ticket",
"Notificacao"
);
$autoload['lib'] = array("buscaCep","fup");
/* ------------------------------------------------------ *
* Carrega a configuração
* ------------------------------------------------------ */
spl_autoload_register(function ($nome) {
global $base_url;
foreach($autoload['Controler'] as $elemento):
if ($elemento == $nome):
$Controller = $base_url."Controller/".$nome.".Controller.php";
if(is_file($Controller)):
$Model = $base_url."Model/".$nome.".Model.php";
if(is_file($Model)):
include_once("$Model");
endif;
include_once("$Controller");
${strtolower($nome)} = new {$nome};
endif;
endif;
$x++;
endforeach;
});
/* ------------------------------------------------------ *
* Inicializar Controllers com o comando ''$nomedoobj = new Controller';
* ------------------------------------------------------ */
//$administrador = new Administrador();
//$ticket = new Ticket();
foreach($autoload['Controler'] as $elemento):
${strtolower($elemento)} = new {$elemento};
endforeach;
?>
Line with error is:
Parse error: syntax error, Unexpected '{' in /home/Andre/public_html/bitbucket/16.4.7/conf.php on line 49
Code:
${strtolower($nome)} = new {$nome};
The name of the file where the class is Controller/Administrator.Controller.php
And here’s the code:
<?php
class Administrador extends AdministradorModel {
function helloworld(){
echo "<h1>Hello World</h1>";
}
}
I imagine the code I typed in line with error would be the theory of how I want it to work, of course, it’s wrong, but how could it be ?
Good evening André, I don’t understand the point of this
${strtolower($nome)}
– Guilherme Nascimento
make php type for me $nomedaclasse = new Nomedaclasse;
– Barack
Yes but the variable will be within the scope of the function, ie it is not being used for anything, got?
– Guilherme Nascimento
But I can take this command out of the function in a foreach inside the conf.php, that would work the theory, but the code would still fail. right?
– Barack
I don’t want to keep loading the classes every time I use it, I don’t want to type again anywhere, I want you to initialize the classes configured in the conf vector itself, as soon as you start conf.php, both the file and the new
– Barack
I understand what you want to do on the basis, but I want to understand where (exact location) you will use the
${strtolower($nome)}
, for there are many ways of doing the same thing. I’m trying to see an alternative solution, but if you don’t explain the objective within the code I can’t give an answer ;)– Guilherme Nascimento
I want to use at the end of confi.php, where it is commented, look at the last line
– Barack
Would be the
$ticket = new Ticket();
?– Guilherme Nascimento
this, the Ticket is hidden there in the vector called $autoload['Controller'], upstairs, I want a foreach to execute the magic command that would be exactly the one that you spoke there, only for each of the vector, so it has to be magic kk.
– Barack
Looking from here it seems that its code in logic fires twice the
new ...
for a call, one at a time$ticket = new Ticket();
and another within the SPL, I think you may not have understood SPL_AUTOLOAD, see if these links help: http://answall.com/a/88039/3635– Guilherme Nascimento
Is that I did running will shoot twice yes have to take it there the load and put in the footer of conf.php, and also take the autoload of initialize function..., It is wrong even I saw now little, but I wanted a way to load the classes of the vector without having to type, is the missing command. PS. I WILL EDIT AND FIX WHAT I HAVE SEEN WRONG, PS2. READY
– Barack
André I think that if you have a little more patience and try to understand spl_autoload and read the link I passed will be able to implement better ;)
– Guilherme Nascimento
At the beginning there is an error, $base_url makes up the path that should be physical, but it is a url, it is virtual... is_file and include should receive a filesystem path (c: www Folder file.php)... first you should start fixing this.
– Daniel Omine