C
C
Cyapa2015-01-06 14:48:44
PHP
Cyapa, 2015-01-06 14:48:44

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.

The implementation of the loader should serve as an example
<?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);
        }
    }
?>

But something tells me that this is not correct. Got a better idea?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
OnYourLips, 2015-01-06
@OnYourLips

so how to create a separate file for each exception is frankly stupid
You are wrong. It's silly not to create a separate class for it.
And store classes in classes/ and traits in traits/ too.
Now I decided to store exceptions next to the class to which they belong.
You're doing some wickedness.
Now answer the question: what do you achieve when you purposefully do not want to follow standards and established practices?
Is it such an obfuscation that no one can figure out the code later?

V
Viktor Vsk, 2015-01-06
@viktorvsk

Isn't it? Why do we need a separate empty class with an exception (Exception)?

A
Alexander Zelenin, 2015-01-06
@zelenin

I will confirm: 1 class - 1 file.
And, yes, take a framework, or at worst a composer for an autoloader.

K
Kirill Boldyrev, 2015-01-06
@usetester

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 question

Ask a Question

731 491 924 answers to any question