什么是OOP中的静态和动态变量/方法?
我试图更好地理解OOP中的基本概念. 面向对象编程中的静态和动态变量和方法是什么? 例如,使用$ this与双冒号(::)之间的区别是什么?
I am trying to better understand basic concepts in OOP. What are static and dynamic variables and methods in object-oriented programming? What is, for instance, the difference between using $this vs. double colon (::)?
- 优点:?.
- 缺点:? ..."this"不是自记录文档,如
$this->method_from_an_extended_class()
.
- 优势:?
- 缺点:?
静态"和动态"不是正确的描述.
"Static" and "dynamic" aren't the right descriptions for that.
->
表示实例函数或实例数据,表示该函数或数据具有隐式的$this
引用.换句话说,您是指特定对象中的函数或变量.
->
indicates a instance functions or instance data, meaning that the function or data has an implicit $this
reference. To put it another way you're referring to the function or variable within a particular object.
::
表示类函数或类变量.这与全局函数或变量非常相似,因为没有隐式$this
引用.该类的所有实例都共享该函数或变量.
::
indicates a class function or class variable. This is very similar to a global function or variable in that there is no implicit $this
reference. All instance of that class share that function or variable.
动态"是对 PHP重载的更准确的描述,在这里您可以动态地例如,使用魔术方法__get()
和__set()
(当您尝试访问找不到的属性时会调用它们;可以重载这些方法以实质上假装所请求的成员存在)来创建变量.
"Dynamic" would be a more accurate description for, say, PHP overloading where you can "dynamically" create variables, for example, using the magic methods __get()
and __set()
(which are called when you try to access a property that can't be found; you can overload these methods to essentially pretend the requested member exists).