说真的,我应该写不好的PHP代码吗? [英] Seriously, should I write bad PHP code?

查看:79
本文介绍了说真的,我应该写不好的PHP代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在做一些PHP工作,在我所看到的所有代码中,人们倾向于使用很少的方法. (他们也倾向于使用很少的变量,但这是另一个问题.)我想知道为什么会这样,并且我发现此注释带有一个参数和一个空函数体的函数调用与执行7-8 $大约需要相同的时间.当然,类似的方法调用大约是15个$ localvar ++操作""此处.

I'm doing some PHP work recently, and in all the code I've seen, people tend to use few methods. (They also tend to use few variables, but that's another issue.) I was wondering why this is, and I found this note "A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations" here.

即使在编译和缓存了PHP页面的情况下,这也是真的吗?我应该避免使用尽可能多的方法来提高效率吗?我喜欢用重复代码块的方法编写井井有条的,易于阅读的代码.如果需要编写不带方法的简单代码,是否有任何程序可以内联"方法主体?这样,我可以编写漂亮的代码,然后在部署之前将其丑陋.

Is this true, even when the PHP page has been compiled and cached? Should I avoid using methods as much as possible for efficiency? I like to write well-organized, human-readable code with methods wherever a code block would be repeated. If it is necessary to write flat code without methods, are there any programs that will "inline" method bodies? That way I could write nice code and then ugly it up before deployment.

顺便说一句,我一直在看的代码来自Joomla 1.5核心和几个WordPress插件,所以我假设他们是知道他们在做什么的人.

By the way, the code I've been looking at is from the Joomla 1.5 core and several WordPress plugins, so I assume they are people who know what they're doing.

注意:,我很高兴每个人都跳过了这个问题,以讨论总体上,但是实际上,我们是在谈论解释型语言中的优化.至少有些暗示我们正在谈论PHP的事实会很好.

Note: I'm pleased that everyone has jumped on this question to talk about optimization in general, but in fact we're talking about optimization in interpreted languages. At least some hint of the fact that we're talking about PHP would be nice.

推荐答案

我认为Joomla和Wordpress并不是 good PHP代码的最好例子,没有冒犯.我对从事此工作的人没有什么私人的看法,这是很棒的方法,他们可以使人们拥有一个网站/博客,而且我知道很多人将所有空闲时间都花在这些项目中,但是代码质量却很差(没有冒犯).

I think Joomla and Wordpress are not the greatest examples of good PHP code, with no offense. I have nothing personal against the people working on it and it's great how they enable people to have a website/blog and I know that a lot of people spend all their free time on either of those projects but the code quality is rather poor (with no offense).

如果您不相信我,请查看过去一年的安全公告;同样假设您正在寻找两者中的任何一个的性能,那么它们的代码也不擅长于此.因此,这绝不是好的代码,但是Wordpress和Joomla都在前端方面表现出色-相当易于使用,人们可以访问网站并可以完成 .

Review security announcements over the past year if you don't believe me; also assuming you are looking for performance from either of the two, their code does not excel there either. So it's by no means good code, but Wordpress and Joomla both excel on the frontend - pretty easy to use, people get a website and can do stuff.

这就是为什么他们如此成功,人们不是根据代码质量而是根据他们能够做什么来选择它们的.

And that's why they are so successful, people don't select them based on code quality but on what they enabled them to do.

要回答您的性能问题,是的,确实所有的好东西(函数,类等)都会使您的应用程序变慢.因此,我想您的应用程序/脚本是否全部在一个文件中,就这样吧.然后随时编写错误的PHP代码.

To answer your performance question, yes, it's true that all the good stuff (functions, classes, etc.) slow your application down. So I guess if your application/script is all in one file, so be it. Feel free to write bad PHP code then.

一旦扩展并开始复制代码,就应该考虑在编写可维护代码方面所带来的折衷(在速度上). :-)

As soon as you expand and start to duplicate code, you should consider the trade off (in speed) which writing maintainable code brings along. :-)

恕我直言,这种权衡相当小,原因有两点:

IMHO this trade off is rather small because of two things:

  1. CPU 很便宜.
  2. 开发人员不便宜.
  1. CPU is cheap.
  2. Developers are not cheap.

如果您需要在六个月后返回代码,请考虑一下,如果节省了十亿分之一秒的代码来运行它,那么当您需要修复一个讨厌的错误(由于重复的代码,需要三到四次)时,仍会加起来.

When you need to go back into your code in six months from now, think if those nano seconds saved running it, still add up when you need to fix a nasty bug (three or four times, because of duplicated code).

您可以做各种事情来使PHP运行更快.通常,人们会建议使用缓存,例如 APC . APC真的很棒.它会在后台为您运行各种优化,例如缓存PHP文件的字节码,还为用户提供了在用户域中保存数据的功能.

You can do all sorts of things to make PHP run faster. Generally people recommend a cache, such as APC. APC is really awesome. It runs all sorts of optimizations in the background for you, e.g. caching the bytecode of a PHP file and also provides you with functions in userland to save data.

因此,例如,如果您每次运行时都解析一个配置文件,那么脚本磁盘的输入/输出就非常关键.使用简单的 apc_store()

So for example if you parse a configuration file each time you run that script disk i/o is really critical. With a simple apc_store() and apc_fetch() you can store the parsed configuration file either in a file-based or a memory-based (RAM) cache and retrieve it from there until the cache expired or is deleted.

当然,APC不是唯一的缓存.

APC is not the only cache, of course.

这篇关于说真的,我应该写不好的PHP代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆