在PHP中使用静态方法和属性会占用更少的内存吗? [英] Does using static methods and properties in PHP use less memory?

查看:577
本文介绍了在PHP中使用静态方法和属性会占用更少的内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,该应用程序每秒可以看到数十个并发用户.我有一个将在同一页面加载中实例化很多次的类.在该类中,我具有一些在每个对象上始终相同的属性,因此我正在考虑将这些属性声明为static,以减少实例化该类的多个实例时将要使用的内存.在同一页面请求期间.

I'm working on a web application that sees dozens of concurrent users per second. I have a class that will be instantiated many times within the same page load. In that class, I have some properties that will always be the same across every object, so I'm thinking about declaring these properties as static in an effort to reduce the memory that will be used when multiple instances of this class are instantiated during the same page request.

这样做会为该应用程序使用更少的内存,因为PHP只能将静态属性的值存储一次吗?这样做会节省并发用户的内存,还是只是在每个PHP进程内?

Will doing this use less memory for this application because PHP can store the value of the static properties only once? Will doing this save memory across concurrent users, or just within each PHP process?

这对方法如何起作用?如果这意味着对象可以回收相同的方法,那么为什么要尝试在内存上进行保存,为什么不将类的所有方法声明为静态方法呢?

How does this work for methods? If this means objects can recycle the same methods, then why wouldn't all methods of a class be declared static if you are trying to save on memory?

我不完全理解为什么以及何时将属性或方法声明为静态,但是我确实知道将它们声明为静态可以在不实例化类的对象的情况下访问它们(这感觉就像是一种hack) ...这些方法和属性应该在其他地方...否?).我对static声明影响内存使用情况的方式特别感兴趣,以尽量降低Web服务器上的内存使用情况……总体而言,这样我对发生的事情有了更好的了解. /p>

I don't feel entirely comfortable with why and when one would declare a property or method static, but I do understand that declaring them as static allows them to be accessed without instantiating an object of the class ( this feels like a hack ... these methods and properties should be somewhere else ... no? ). I'm specifically interested in the way a static declaration affects memory usage in an effort to keep memory usage as low as possible on my web server ... and in general so I have a better understanding of what is going on.

推荐答案

将类方法/变量声明为静态时,它绑定到该类而不是对象,并由该类共享.从内存管理的角度来看,这意味着将类定义加载到堆内存时,将在此处创建这些静态对象.当在堆栈存储器中创建类的实际对象并且完成对静态属性的更新时,指向包含静态对象的堆的指针将被更新.这确实有助于减少内存,但不会减少很多.

When you declare a class method/variable as static, it is bound to and shared by the class, not the object. From a memory management perspective what this means is that when the class definition is loaded into the heap memory, these static objects are created there. When the class's actual object is created in the stack memory and when updates on the static properties are done, the pointer to the heap which contains the static object gets updated. This does help to reduce memory but not by much.

从编程范例来看,人们通常选择使用静态变量来获得体系结构上的优势,而不是使用内存管理优化.换句话说,当您想实现单例或工厂模式时,可能会像您提到的那样创建静态变量.它提供了更强大的方法来了解类"级别发生的事情,而不是对象"级别发生的事情.

From a programming paradigm, people usually choose to use static variables for architectural advantages more than memory management optimization. In other words, one might create static variables like you mentioned, when one wants to implement a singleton or factory pattern. It provides more powerful ways of knowing what is going on at a "class" level as opposed to what transpires at an "object" level.

这篇关于在PHP中使用静态方法和属性会占用更少的内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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