This time I will show you how to use PHP static binding in the project, and what are the precautions for using PHP static binding in the project. The following is a practical case, let's take a look.
Basic knowledge
1. Range parsing operator (::)
can be used It is used to access static members and class constants, and can also be used to override properties and methods in the class.
The three special keywords self, parent and static are used to access its properties or methods inside the class definition.
parent is used to call overridden properties or methods in the parent class (where it appears, it will be resolved to the parent class of the corresponding class).
self is used to call methods or properties in this class (wherever it appears, it will be parsed into the corresponding class; note the difference with $this, $this points to the currently instantiated object ).
When a subclass overrides a method in its parent class, PHP will not call the overridden method in the parent class. Whether the method of the parent class is called depends on the child class.
2. The PHP kernel places the inheritance implementation of classes in the "compilation phase"
Copy after login
Running results:
AA
AA
Conclusion:
self:: and parent:: appear in the definition of a certain class X, they will be parsed into the corresponding class X, unless the parent class method is overridden in the subclass.
3.Static (static) keyword
Function:
- The static keyword is used to modify variables in the function body Define static local variables.
- Used to declare static members when modifying class member functions and member variables.
- (After PHP5.3) A special class that represents static delayed binding before the scope resolver (::).
Example:
Define static local variables (occurrence: in local functions)
Features: Static variables only exist in the local function domain, but when the program Its value is not lost when execution leaves this scope.
Copy after login
Define static methods, static attributes
a) Declare class attributes or methods as static, so you can access them directly without instantiating the class.
b) Static properties cannot be accessed through an instantiated object of a class (but static methods can)
c) If access control is not specified, properties and methods default to public.
d) Since static methods do not require an object to be called, the pseudo variable $this is not available in static methods.
e) Static properties cannot be accessed by objects through the -> operator.
f) Calling a non-static method statically will result in an E_STRICT level error.
g) Like all other PHP static variables, static properties can only be initialized to literals or constants, not expressions. So a static property can be initialized to an integer or an array, but it cannot be initialized to another variable or function return value, nor can it point to an object.
a. Static method example (occurrence: class method definition)
b. Static attribute example (occurrence: class attribute definition)
staticValue() . "\n"; print $foo->my_static . "\n"; // Undefined "Property" my_static print $foo::$my_static . "\n"; $classname = 'Foo'; print $classname::$my_static . "\n"; // As of PHP 5.3.0 print Bar::$my_static . "\n"; $bar = new Bar(); print $bar->fooStatic() . "\n"; ?>
c. Used for late static binding (position: in class methods, used to modify variables or methods)
Detailed analysis below
Late static binding (late static binding)
Since PHP 5.3.0, PHP has added a feature called late static binding, which is used to reference statically called classes in the inheritance scope.
1. Forwarded calls and non-forwarded calls
Forwarded calls:
refers to static calls made in the following ways: self: :, parent::, static:: and forward_static_call().
Non-forwarded calls:
Static calls that explicitly specify the class name (such as Foo::foo())
Non-static calls (such as $foo->foo( ))
2. Working principle of late static binding
Principle: The class name in the previous "non-forwarding call" is stored . This means that when we call a static call that is a forward call, the class actually called is the class of the previous non-forward call.
例子分析:
Copy after login
3.更多静态后期静态绑定的例子
a)Self, Parent 和 Static的对比
selfname() . "\n"; echo $apple->parentname() . "\n"; echo $apple->staticname(); ?> 运行结果: Mango Orange Apple
b)使用forward_static_call()
运行结果: Orange is Orange is my favorite fruit Apple is my father's favorite fruit
c)使用get_called_class()
运行结果: Mango Orange
应用
前面已经提到过了,引入后期静态绑定的目的是:用于在继承范围内引用静态调用的类。
所以, 可以用后期静态绑定的办法解决单例继承问题。
先看一下使用self是一个什么样的情况:
Copy after login
通过上面的例子可以看到,使用self,实例化得到的都是类A的同一个对象
再来看看使用static会得到什么样的结果
Copy after login
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to use PHP static binding in your project. For more information, please follow other related articles on the PHP Chinese website!