在PHP应用程序中可能有太多的函数吗? [英] Is it possible to have too many functions in a PHP application?

查看:86
本文介绍了在PHP应用程序中可能有太多的函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP应用程序是否有太多功能?大量PHP函数的执行是否会占用内存和资源?我的WordPress主题在开发中有很多功能(当我完成时可能有100多个功能),我担心我可能会有太多功能。

解决方案

我不会关心100个函数。这是很少的功能。



全局函数和方法被编译,存储在内存中,并在散列表中进行索引(保存用于最近实现的缓存查找)。因为访问哈希表的时间平均在一段时间内完成,所以调用函数的次数不会减少性能。



但是,将有更多的开销解析脚本,如果你实际调用所有这些函数,编译它们。如果你使用操作码缓存,这不是一个真正的问题。还会有更多的内存开销,但通常情况下,内存在企业级Web服务器中并不是问题,因为尽可能快地尝试为请求提供服务,而不必关心内存。 b
$ b

还有一些风格问题。如果您的全局函数太多,请考虑:


  • 您正在复制这些函数之间的代码。考虑重构,将通用代码移动到其他函数,并在适当的情况下通过添加参数来推广函数的行为。
  • 您可以更好地对类中相同数据进行操作的分组函数。


最后,只有在您对应用程序进行配置并查找函数调用为CPU瓶颈和函数定义成为内存瓶颈。

Can a PHP application have too many functions? Does the execution of a large number of PHP functions hog memory and resources? My WordPress theme in-development has a lot of functions (probably more than 100 by the time I'm done) and I'm concerned that I might have too many.

解决方案

I wouldn't be concerned about 100 functions. That's very few functions.

Global functions and methods are compiled, stored in memory, and indexed in a hash table (save for cached lookups implemented recently). You won't have performance deterioration when calling the functions as the number of functions grows, since accessing the hash table is done, on average, in constant time.

However, there will be more overhead parsing the scripts and, if you actually call all those functions, compiling them. This is not really a problem if the you use an opcode cache. There will also be more memory overhead, but typically, memory is not a problem in enterprise grade web servers, where it's more appropriate to try to serve the requests as fast as possible, without caring that much about the memory.

There are also stylistic issues. If you have too many global functions, consider whether:

  • You are duplicating code between those functions. Consider refactoring, moving the common code to other function and generalizing the behavior of the functions by adding parameters, where appropriate.
  • You would be better grouping functions that operate on the same data in an class.

In the end, worry about this only if you profile your application and find function calls to be a CPU bottleneck and function definitions to be a memory bottleneck.

这篇关于在PHP应用程序中可能有太多的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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