What is the professional point of view for including a basic PHP script in all other PHP scripts?

I can imagine that in large projects, some things usually become redundant in most PHP scripts. From the head: Including classes, authentication, including the configuration file, the parameter includes the path, etc.

As for my imagination, this should be done absolutely in every PHP script in the project. This would be simplified by adding a β€œcore” PHP script that handles all this.

However, from this very site I can quote

"I plan to create a PHP file" core.php "that will be included at the beginning of every EVERY SINGLE PHP file in the project. This file will handle authentication and include basic functions. Thoughts?"

I cannot stress enough, "do not do this." There is a rule among experienced PHP developers that any project with a large core.php file that is a warning sign of poor development should be avoided.

A source

Which leaves me at a loss. Is it better to redundantly write the same 20-30 lines of code on top of each file than to use DRY encoding?

Any clarifications would be appreciated!


I will quickly clarify here. The "front controller template" that I actually use when writing most websites and applications does not really match the type of project I'm talking about. Well, actually it is, and I intend to use it, but in my project there are also a lot of PHP scripts that should return content for Ajax requests. These are the PHP scripts that my question concerns.

+4
source share
3 answers

If you need to include 20-30 lines on top of each page, this sounds like time for a better architecture. For example, see Dispatcher / Routing. Each request is processed by a central .php file, Dispatcher, which analyzes the request and decides which files to call and load.

This is implemented in most PHP frameworks. Play with one to feel it.

0
source

I recommend using the same approach as Wordpress, the Front Controller template.

It basically filters all incoming page requests through index.php. Open .htaccess and you will see that it filters all requests using index.php if the file or directory no longer exists. This allows you to parse the URL into sections of any syntax you need. No need to create different files for different URLs. You can get example.com/page/1 and display the section at the end of any page.

Kohana is a great library that tries to understand and comprehend this concept. This allows you to extend classes and implement many of the features of PHP 5. As an added bonus, Kohana has MVC (also HMVC), which is incredibly important for large sites.

+4
source

I do not believe that the answer was against DRY, although this does not make it easy to see. The author really suggested using the established structure, which, of course, takes care of the initialization and general functionality of the application in a centralized and modular way.

Perhaps the author had in mind "not to release a large spaghetti-ball code"; this may be in practice an ill-conceived attempt to build a framework by grouping boat loads of the main methods in a monolithic script.

If you are building (at least a large part) to find out, I find nothing wrong with trying to centralize your main functions, organize them, and start creating fledgling frames this way. By doing this in a thoughtful way, you will gain invaluable hands-on experience and an understanding of how applications in general can be archived. Otherwise, I will answer the author of this answer: why does your application suffer from possible incorrect design decisions when there are many fantastic frameworks ready to use?

+1
source

Source: https://habr.com/ru/post/1341251/


All Articles