8
I have two codes, the first one uses spl_autoload_register
and the other does not, however the second loads "automatically the class" also.
With spl_autoload_register
- Uses namespaces to split MVC
- You can create multiple levels of folders by following the idea of PSR-4
- Controllers and Models may have the same name since each is within a different namespace.
Code:
spl_autoload_register(function ($class)
{
$np = explode('\\', $class);
$base = strtolower($np[0]);
switch ($base) {
case 'controller':
case 'model':
$base = 'application/' . $base . 's';
break;
default:
return NULL;
}
array_shift($np);
$relative_class = strtolower(implode('/', $np));
$file = './' . $base . '/' . $relative_class . '.php';
/*
* resulta em algo como:
* ./application/controllers/foo/test/user.php
* ./application/models/foo/abc/user.php
*/
if (is_file($file)) {
require_once $file;
}
});
Calling an action from a controller:
$controller = new \Controller\foo\test\user;
$controller->profile();
Calling a model:
$model = new \Model\foo\test\user;
With methods
- Does not use namespaces
- "Eventually" may be easier to understand/use than the previous code
- Controllers and Models may not have the same name, but it’s no problem, since we can use prefixes
- Supports sub-folders.
Code:
<?php
class App
{
static private function prepare($path)
{
$fp = explode('.', $path);
return array(
'name' => end($fp),
'path' => implode('/', $fp)
);
}
static public function model($name)
{
$data = self::prepare($name);
if (is_file($data['path'])) {
require_once './application/models/' . $data['path'] . '.php';
}
return new $data['name'];
}
static public function action($name, $action)
{
$data = self::prepare($name);
if (is_file($data['path'])) {
require_once './application/controllers/' . $data['path'] . '.php';
}
$controller = new $data['name'];
$controller->$action;
}
}
Calling an action from a controller:
App::action('foo.test.user', 'profile');
Calling a model:
$model = App::model('foo.abc.user');
My question is, should I use the simplest way without namespaces and spl_autoload
or not? How can I work or improve these codes so that it makes use easier for the ultimate developer?
Could you explain this point simple string strongly coupled to its folder structure., with psr-4 is not nearly the same thing, only namespaces coupled to folder structure?
– Guilherme Nascimento
I didn’t understand it either PHP will not automatically load the classes needed for your application, it does load the . php file automatically. I didn’t get this point either.
– Guilherme Nascimento
I’ll edit the answer here calmly @Guilhermenascimento.
– gmsantos
I understand now, about this limited use of object orientation resources, this was the most important point of his reply and certainly the best, I did not know this
class Foo \Controller\exemplo {}
fires thespl_autoload
, tested and worked. -- I miss my reading the php documentation rs– Guilherme Nascimento
Could put an example (without the need to write spl code)
class baz extends bar
explaining thatextends bar
also works with spl. Other than this my first code I did based on psr-4 (Closure Example), could make any suggestion/review/criticism of it?– Guilherme Nascimento
@Guilhermenascimento is all right in using the clousure along with the
spl_autoload_register
. The only comment I have is that its code is not compatible with versions between PHP 5.1 (where SLP was introduced) and version 5.3 (which is where clousures appeared). Other than that I don’t see a problem.– gmsantos
but I tested the code in php5.3 and it worked normal, only it doesn’t work in php5.0 until 5.1 because these don’t support namespaces.
– Guilherme Nascimento
True, since namespaces are used, the code already requires the minimum version to be 5.3. If you were to use only spl_autoloader (without clousure) that would be supported in 5.1 and 5.2
– gmsantos
correcting my last comment, 5.0, 5.1 and 5.2. : ) Thanks for the tip from
extends
,implements
, etc was what made me decide.– Guilherme Nascimento
Just to comment, so when you said it works on 5.3, I did it without the clousure after. Thank you
– Guilherme Nascimento