require() 是不好的做法吗?如何不重用代码
大量使用 require() 是不好的做法吗?大多数网站都有页眉和页脚,无论您在页面上的哪个位置导航,它们看起来都一样。但各页的主要内容有所不同。由于我不想重用代码,所以在两个地方使用相同的代码,我认为 require() 非常好。但是像我这样使用可以吗?
这是我的例子:
<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>
侧边栏框是一个包含“最新新闻”等内容的框。由于我可能希望在许多页面上都有“最新新闻”框,所以我需要 require() 它以避免两个地方有相同的代码。
我应该以其他方式解决它吗?
感谢您的帮助!
Is it bad practice using require() a lot? Most websites have a header and a footer that looks the same whereever you navigate on the page. But the main content varies from page to page. And since I don´t want to reuse code, have the same code in two places, I think require() is pretty good. But is it okay using it like I do?
Here´s 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>
A sidebar box is a box that contains stuff like "Latest news" etc. And since I may want to have the "Latest news" box on many pages I need to require() it in order to not have the same code in two places.
Should I solve it someway else?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这看起来完全没问题,并且正是
require()
的构建目的。使用
require()
并不是很好的做法的情况通常是在 PHP 端 - 例如获取立即执行的大块 PHP 代码。在这些情况下,拥有一个包含函数或类的适当库是更好的主意。但是使用require()
获取重复的页面部分是可以的。That looks totally okay and is exactly what
require()
was built for.Situations where using
require()
isn't great practice are usually on the PHP end - e.g. fetching big blocks of PHP code that are executed straight away. Having a proper library with functions or classes is the better idea in those cases. But fetching repetitive page sections usingrequire()
is fine.不,当然不是。
require()
是一个很好的实践,特别是当您想在用户与页面交互之前进行一些控制时。例如,您可以将数据库连接数据或用户会话控制数据保存在单独的 PHP 文件中,并在向用户提供特定页面之前需要它们。通过这种方式,您可以重用代码并促进您的工作。No, of course not.
require()
is a good practice especially when you want to make some controls before make the user interact with your pages. For instance, you can keep database connection data or user session control data in separate PHP files and require them before providing your specific pages to the users. In that way, you reuse code and facilitate your work.这完全没有问题,重用代码块是非常好的做法。它还使 HTML 结构更具可读性,并为您提供 HTML 表示和 PHP 业务逻辑之间的一些基本分离。查看 MVC 模式 了解为什么会这样一件好事。
需要记住的一些要点:
Nothing wrong with that at all, it's very good practice to reuse code chunks. It also keep the HTML structure far more readable and gives you some basic separation between HTML presentation and PHP business logic. Check out the MVC pattern for why this is a good thing.
Some points to bear in mind:
是的,您概述的原则是好的。避免代码重复并重复使用这样的“块”总是好的。我想说你应该避免重新发明轮子。您可能想要研究一个模板系统来帮助您管理为构建网站而创建的“块”。我不会尝试自己构建一个,而是会调查已经存在的(Twig 和 Smarty 是我想到的两个,还有其他)。即使您决定不使用它,查看它并了解它是如何组合在一起的也会对您构建自己的产品有所帮助。
Yes, the principles you're outlining are good ones. It's always good to avoid code duplication and re-use "chunks" like this. I would say that you should avoid re-inventing the wheel. You might want to investigate a templating system to help you manage the "chunks" you're creating to build your site. Rather than try and build one yourself, I would investigate the ones out there already (Twig and Smarty are two that spring to mind, there are others). Even if you decide not to use one, looking at it and seeing how it's put together will help you when you're building your own.