Ecosystem: PHP 7.1, self-written project, I am the main developer, C / C ++ programmer

There is a project that has a banal structure:

  • index.php (+ mod_rewrite all URLs to it)
  • / classes (lots of files in folders and subfolders describing functions and classes)

The project is working, everything is in order. However, there is a problem - the number of files is growing. I connect each new file through require_once(...) and they have already accumulated under a hundred. It didn't bother me

The other day there was a task to integrate into my development project of past years, which are designed as a hierarchy of classes, of which about 50+, each class in its own file, in its namespace, divided into subfolders, everything is very logical and understandable

I made a find . -name "*.php" in the forehead find . -name "*.php" find . -name "*.php" and put everything into requre_once() , as I usually did. I checked that PHP’s traces started to pour in on the fact that some class needs another class from which this one is inherited. By magical permutations of the order of connection, I achieved that it all worked

But I think this approach is not scientific, not beautiful

Question:

  1. How in PHP it is accepted to solve problems of dependences between classes and the order of connection of files?
  2. How is this connection implemented in PHP? It is desirable to native version, rather than a variant of a specific framework.

Two points of interest: connection speed / code execution speed and implementation convenience / security

I will make a reservation in advance - please, do not refer to the implementation of this mechanism in popular frameworks. I am sure that they are great and wonderful, but it is exactly Vanilla PHP 7 with standard libraries that are connected via .so.

    1 answer 1

    Try autoloading classes: http://php.net/manual/ru/language.oop5.autoload.php Here is an example:

     <?php spl_autoload_register(function ($class_name) { include $class_name . '.php'; }); $obj = new MyClass1(); $obj2 = new MyClass2(); ?> 

    Those. you register a function that will load your files with classes. In this example, when new MyClass1 () is executed, it will automatically include MyClass1.php. Similarly for MyClass2 () will include MyClass2.php

    • That is, you need to register a certain function to which the name of the class will be transferred, and in it you must already implement the search for the necessary file anywhere and in any way? For example, you can create an array of values classname => filename or something like that - wirtwelt
    • Yes, you understood everything correctly. Here are some examples of working with autoloading: codengineering.ru/post/28 - Diver
    • Oh, great, I asked the question in vain, I had to sit in Google for another 5 minutes) Thank you - wirtwelt
    • @Diver, supplement the question with examples of code, please) Unfortunately, a message containing only a link cannot be considered an answer according to the rules of the site ( - YuriySPb