Need () bad practice? How not to reuse code

Is it a bad practice using require () a lot? Most websites have a header and footer that looks the same as you navigate the page. But the main content varies from page to page. And since I don't want to reuse the code, I have the same code in two places, I think require () is pretty good. But is it good to use like me?

Here is my example:

<div class="content"> <section> <?php require('mainpage.php'); ?> </section> <aside class="sidebar"> <div class="sidebar-box"> <?php require('sidebar/box1.php'); ?> </div> <div class="sidebar-box"> <?php require('sidebar/box2.php'); ?> </div> </aside> </div> 

Sidebar is a field that contains materials such as Latest News, etc. And since I can have the Latest News window on many pages, I need to require () not to have the same code in two places.

Should I solve it somehow else?

Thanks for your help!

+4
source share
4 answers

There is nothing wrong with that; it is very good practice to reuse pieces of code. It also keeps the HTML structure much more readable and gives you some basic separation between the HTML representation and the PHP business logic. Check out the MVC pattern why this is good.

Some points to consider:

  • You might want to consider the $ docroot variable, which has the full path to the server file, since relative file paths can sometimes depend on some PHP settings, as well as if you included this code from another file. Relative file paths look like it's easier to move the entire codebase, but you can just change $ docroot if you do, and that's fine. You can also use the $ librarydir variable so your HTML templates can move around without changing the location of the PHP files.
  • Some files must be delivered using require_once (), so you wonโ€™t get a mess, for example. HTML header, footer, etc. It may seem obvious, but itโ€™s easy to place the footer in two files, which are then included from another location, so this will facilitate debugging by providing a more detailed error message.
  • You may find that the template engine, which makes the MVC approach more flexible, more formal, can help you. Check out Smarty , as others have mentioned, or Dwoo , which is a rewrite of PHP5.
  • Odd things can happen if the code in the included files uses variables in the global scope, and as your program becomes more complex, it can lead to hard-bound errors, where one of them is a subtle violation of the other by changing the variables with by the same name. Consider putting the code for the side block in a function or class so that the variables are in their own scope and you can save them separately.
0
source

This looks completely fine, and that is exactly what require() was created for.

Situations in which using require() not a big practice are usually at the end of PHP. extraction of large blocks of PHP code that are executed immediately. Having a proper library with functions or classes is the best idea in these cases. But choosing repeatable page sections with require() great.

+4
source

No, of course not. require() is good practice, especially if you want to make some controls before getting the user to interact with their pages. For example, you can save database connection data or user session control data in separate PHP files and require it before providing its specific pages to users. This way you reuse the code and make your work easier.

+3
source

Yes, the principles you have outlined are good. It is always helpful to avoid code duplication and reuse of โ€œchunksโ€ like this. I would say that you should avoid reinventing the wheel. You can study the template system to help you manage the โ€œpiecesโ€ that you create to create your site. Instead of trying to build it yourself, I would investigate the ones already there (Twig and Smarty are two that spring, to keep in mind, there are others). Even if you decide not to use it, looking at it and seeing how it is going to help you when you create your own.

0
source

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


All Articles