>  기사  >  백엔드 개발  >  关于PHP中方法中的static的问题

关于PHP中方法中的static的问题

WBOY
WBOY원래의
2016-06-06 20:36:56862검색

static用在类的方法中,我现在的问题是static的这个变量是保存在内存中,
1 具体保存在内存中的哪个地方?
2 这个static的变量多长时间被收回?
3 过多的使用的static会不会造成服务器内存不够?
4 不同的用户的static变量为什么会互不影响?

回复内容:

static用在类的方法中,我现在的问题是static的这个变量是保存在内存中,
1 具体保存在内存中的哪个地方?
2 这个static的变量多长时间被收回?
3 过多的使用的static会不会造成服务器内存不够?
4 不同的用户的static变量为什么会互不影响?

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.

static的是保存在内存中的吧,过多的使用应该会造成内存不足?

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.