require() 是不好的做法吗?如何不重用代码

发布于 2025-01-07 00:45:00 字数 716 浏览 2 评论 0原文

大量使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

表情可笑 2025-01-14 00:45:00

这看起来完全没问题,并且正是 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 using require() is fine.

与风相奔跑 2025-01-14 00:45:00

不,当然不是。 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.

不再见 2025-01-14 00:45:00

这完全没有问题,重用代码块是非常好的做法。它还使 HTML 结构更具可读性,并为您提供 HTML 表示和 PHP 业务逻辑之间的一些基本分离。查看 MVC 模式 了解为什么会这样一件好事。

需要记住的一些要点:

  • 您可能需要考虑使用具有完整服务器文件路径的 $docroot 变量,因为相对文件路径在某些 PHP 安装中有时会变得不稳定,而且如果您从另一个文件包含上述代码本身。相对文件路径看起来更容易移动整个代码库,但如果你这样做的话,你可以只改变 $docroot 就可以了。您还可以使用 $librarydir 变量,以便可以移动 HTML 模板而无需移动 PHP 文件的位置。
  • 有些文件应该用 require_once() 带来,这样你就不会弄乱,例如 HTML 页眉、页脚等。可能听起来很明显,但很容易将页脚放入两个文件中,然后从其他地方包含它们,因此可以通过提供更详细的错误消息来使调试变得更容易。
  • 您可能会发现以更正式、更灵活的方式执行 MVC 方法的模板引擎可以帮助您。正如其他人提到的,请查看 Smarty ,或 Dwoo,这是它的 PHP5 重写。
  • 如果包含文件中的代码使用全局范围内的变量,则可能会发生奇怪的事情,并且随着程序变得更加复杂,这可能会导致难以确定的错误,其中一个包含文件通过使用同名。考虑将 sideblock 的代码放入函数或类中,以便变量位于其自己的范围内,并且可以将它们分开。

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:

  • You might want to consider using a $docroot variable that has the full server file path, as relative file paths can occasionally go wonky on some PHP installs and also if you include the above code itself from another file. Relative file paths look like it's easier to move the whole codebase around, but you can just alter $docroot if you do this and it's fine. You could also use a $librarydir variable so that your HTML templates can be moved around without shifting the location of the PHP files.
  • Some files ought to be brought it with require_once() so that you don't get a mess e.g. the HTML header, page footer etc. Might sound obvious, but it's easy to put the footer into two files that you then include from somewhere else, so this can make debugging easier by giving a more verbose error message.
  • You may find that a templating engine which does the MVC approach in a more formalised an flexible way could help you. Check out Smarty as others have mentioned, or Dwoo, which is a PHP5 rewrite of it.
  • Odd things can happen if the code in the included files uses variables in the global scope, and as your program becomes more complex, this can lead to hard-to-pin-down bugs where one include is subtly breaking another by altering variables with the same name. Consider putting the code for a sideblock into a function or class so that variables are in their own scope and you can keep them separate.
柏拉图鍀咏恒 2025-01-14 00:45:00

是的,您概述的原则是好的。避免代码重复并重复使用这样的“块”总是好的。我想说你应该避免重新发明轮子。您可能想要研究一个模板系统来帮助您管理为构建网站而创建的“块”。我不会尝试自己构建一个,而是会调查已经存在的(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文