Answer the question
In order to leave comments, you need to log in
How to organize autoloading in PHP without PSR?
Gentlemen, I need your help organizing the architecture.
I am writing a small PHP application using autoloading and namespaces.
In the beginning, everything went well: classes are stored in the /classes/ directory, traits in the /traits/ directory . The root namespace indicates belonging to a particular type. There were no problems with this.
But now the turn has come to the exception classes ... It is clear that they need to be stored somehow in a special way, because creating a separate file for each exception is frankly stupid. But what to do?
Now I decided to store exceptions next to the class to which they belong. The root namespace for them will be Exceptions, followed by the name of the class holder.
<?php
namespace Exceptions\Core\Autoloader;
class ErrorAutoload extends \Exception{}
class ErrorAutoloadRegistry extends ErrorAutoload{}
class ErrorAutoloadInclude extends ErrorAutoload{}
namespace Core;
use \Exceptions\Core\Autoloader\ErrorAutoloadRegistry;
use \Exceptions\Core\Autoloader\ErrorAutoloadInclude;
class Autoloader
{
use \Traits\Singleton;
protected function __construct()
{
if(!spl_autoload_register(array($this, 'autoload'), true))
{
throw new ErrorAutoloadRegistry();
}
}
public function autoload($object_name)
{
$path = str_replace('\\', DIRECTORY_SEPARATOR, $object_name);
$path = strtolower($path);
switch(substr($path, 0, strpos($path, DIRECTORY_SEPARATOR)))
{
case 'classes':
case 'traits':
break;
case 'exceptions':
$path = substr($path, strpos($path, DIRECTORY_SEPARATOR) + 1);
$path = dirname($path);
default:
$path = 'classes' . DIRECTORY_SEPARATOR . $path;
break;
}
$path = APPLICATION_ROOT . DIRECTORY_SEPARATOR . $path . '.php';
if(!file_exists($path))
{
throw new ErrorAutoloadInclude($object_name);
}
require_once($path);
}
}
?>
Answer the question
In order to leave comments, you need to log in
so how to create a separate file for each exception is frankly stupidYou are wrong. It's silly not to create a separate class for it.
Now I decided to store exceptions next to the class to which they belong.You're doing some wickedness.
Isn't it? Why do we need a separate empty class with an exception (Exception)?
I will confirm: 1 class - 1 file.
And, yes, take a framework, or at worst a composer for an autoloader.
1 exception class - 1 file. I made exceptions for exceptions that were used by 1 particular class and nowhere else, and kept them in the file of this class, which did not affect autoloading in any way. But then I changed my mind - because when using the opcode cache, the presence of 10 extra class files does not have any effect on performance at all, and maintaining the code is a little easier. The latter, IMHO, is much more important than all other considerations, except for performance.
PS. The idea of not using PSR is fundamentally flawed. Just don't confuse the idea of not using an industry standard with the idea of not using a popular framework. They are different ideas :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question