简体   繁体   English

是否存在过度使用PHP的include()这样的事情?

[英]Is there such a thing as an over use of PHP's include()?

I am using includes to pull in the various functions I am using, and I am now starting to use include to pull in chunks of HTML/PHP. 我正在使用includes来提取我正在使用的各种函数,现在我开始使用include来拉入大量的HTML / PHP。 Is there a point where I have overused includes? 有没有一点我过度使用包括?

As soon as you start having problems reading your own code that you wrote some time ago, it's definitely too much. 一旦你开始阅读你之前写的自己的代码时出现问题,那肯定是太多了。

I recommend programming in object oriented PHP and using autoloaders to avoid include/require as far as possible. 我建议使用面向对象的PHP进行编程,并使用自动加载器尽可能避免使用include / require。 Excessive use of include/require often leads to unreadable and unmaintainable spaghetti code, which is very bad. 过度使用include / require通常会导致难以理解且难以维护的意大利面条代码,这非常糟糕。

In small projects I usually just have one require statement to pull in my autoloader function(s) and in larger applications I use Zend Framework where I rely on Zend_Loader exclusively. 在小项目中,我通常只有一个require语句来引入我的自动加载器功能,而在大型应用程序中,我使用Zend Framework,我依赖Zend_Loader。

From a purist point of view I'd say: More than 3 includes/requires in your own code (without third party libs) is too much: 从纯粹主义的角度来看,我会说:在你自己的代码(没有第三方库)中,超过3个包含/要求太多了:

  1. One for inluding some iniitialization stuff 一个用于包含一些初始化的东西
  2. One for loading the autoloader class/function 一个用于加载自动加载器类/功能
  3. And the one in the autoloader itself. 而自动加载器本身就是一个。 There should only be one function that actually incudes/requires files. 应该只有一个实际包含/需要文件的功能。 That function or method can then be reused in extended autoloader classes. 然后可以在扩展的自动加载器类中重用该函数或方法。

I mostly try to stick to that principle. 我大多试图坚持这个原则。

I'd say it depends to what point your code is still readable. 我会说这取决于你的代码仍然可读的点。 If someone not working on your project have difficulties to understand your code then yes, includes are overused. 如果没有在您的项目上工作的人难以理解您的代码,那么是的,包括过度使用。

You can overuse anything but it's probably not doing you that much harm (just a few extra stats here and there). 你可以过度使用任何东西,但它可能不会对你造成太大的伤害(这里和那里只是一些额外的统计数据)。 You have to remember that large projects like Drupal and Wordpress do hundreds, if not thousands of include s. 你必须记住像Drupal和Wordpress这样的大项目会做数百个,如果不是数千个include s。

If you're hooking in HTML, you might be getting a bit desperate. 如果您正在使用HTML,您可能会有点绝望。 I'd personally have a good look at a proper templating language or even a framework that helped you into a MVC or MVT stance. 我个人非常了解一个正确的模板语言,甚至是一个帮助你进入MVC或MVT立场的框架。 It makes maintaining it a lot easier than chasing includes all over the place and (more importantly), keeps 95% of your logic out of your presentation files. 它使得维护它比在整个地方进行追逐更容易,并且(更重要的是)将95%的逻辑保留在演示文件中。 Oh and they can maintain your databases in a much more programmatic modular method. 哦,他们可以用更加程序化的模块化方法维护您的数据库。

Basically Frameworks give you a lot of development benefits ;) 基本上框架为您提供了很多开发优势;)

Symphony and CakePHP are both good frameworks but if you just want a look at templating, have a go with Smarty . SymphonyCakePHP都是很好的框架,但是如果你只是想看一下模板,请跟Smarty一起去。

If all you are using is includes then I would look into another way of doing it. 如果您使用的只是包含,那么我会研究另一种方法。

For example if you have a separate file for every function maybe look into putting them all in one file or putting them with similar functions. 例如,如果每个函数都有一个单独的文件,那么可以考虑将它们全部放在一个文件中,或者将它们放在类似的函数中。

It's really a matter of architecture and optimisation. 这真的是建筑和优化的问题。 Rather than discuss what's the optimal number of includes per script, I'd advise using a template engine, eg Smarty because it allows you to: 我不建议讨论每个脚本的最佳包含数量,而是建议使用模板引擎,例如Smarty,因为它允许您:

  1. Separate markup from the program logic 将标记与程序逻辑分开
  2. Use template tags and built-in functions to considerably ease the development 使用模板标签和内置函数可以显着简化开发
  3. Cache preprocessed PHP files making the whole thing a lot faster for your users 缓存预处理的PHP文件,使您的用户更快地完成整个过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM