


How to form static binding between static:: and new static() in PHP
static in PHP::late static binding with new static()
1. parent, self, $this, __CLASS__
class A {} class B extends A{ parent::(public|protected)(静态方法|静态变量) ===> parent永远是A self::(public|protected)(静态方法|静态变量) ===> self永远是B $this->(public|protected)(非静态方法|非静态变量) ===> $this永远是B的是实例化对象 __CLASS__ ===> 永远是B } class C extends B{ parent::(public|protected)(静态方法|静态变量) ===> parent永远是B self::(public|protected)(静态方法|静态变量) ===> self永远是C $this->(public|protected)(非静态方法|非静态变量) ===> $this永远是C的是实例化对象 __CLASS__ ===> 永远是C }
2. static::
The static keyword can achieve the following functions:
1 Calling the static method of the class has a late static binding effect;
2 Calling the static property of the class has a late static binding effect;
3 Calling the non-static method of the class has no late static binding effect ;
4 Note: Non-static properties cannot be called;
class A { private static function foo() { echo "A success!\n"; } public function test() { $this->foo(); } } class B extends A { } class C extends A { private static function foo() { echo "C success!\n"; } } $b = new B(); $b->test();//A success! $c = new C(); $c->test();//A success!
class A { private static function foo() { echo "A success!\n"; } public function test() { static::foo(); } } class B extends A { } class C extends A { private static function foo() { echo "C success!\n"; } } $b = new B(); $b->test();//A success! $c = new C(); $c->test();//A无法调用C里的私有foo方法 //将C的foo改成非private(public|protected)就可以解决 class A { private static function foo() { echo "A success!\n"; } public function test() { static::foo(); } } class B extends A { } class C extends A { public static function foo() { echo "C success!\n"; } } $b = new B(); $b->test();//A success! $c = new C(); $c->test();//C success!
class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } public static function who() { echo __CLASS__."\n"; } } class C extends B { public static function who() { echo __CLASS__."\n"; } } C::test(); A =>A::foo()的结果 C =>parent::foo()能走到A的foo,里面static::who找C::who C =>self::foo()能走到B的foo,B继承A,走到A的foo,里面static::who找C::who class A { protected static function foo() { static::who(); } protected static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } protected static function who() { echo __CLASS__."\n"; } } class C extends B { protected static function who() { echo __CLASS__."\n"; } } C::test(); //A C C,解释同上 class A { public static function foo() { static::who(); } private static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); // self::foo(); } private static function who() { echo __CLASS__."\n"; } } class C extends B { private static function who() { echo __CLASS__."\n"; } } C::test(); //A =>A::foo()的结果 //报错 A不可C的私有方法who => parent::foo()能走到A的foo,里面static::who找C::who,C的who只能在C里调用,不能在A里调用 //报错 A不可C的私有方法who => self::foo()能走到B的foo,B继承A,走到A的foo,里面static::who找C::who,C的who只能在C里调用,不能在A里调用
3. new static()
//new self()与new static()的区别,官网例子如下: class A { public static function get_self() { return new self(); } public static function get_static() { return new static(); } } class B extends A {} echo get_class(B::get_self()); // A echo get_class(B::get_static()); // B echo get_class(A::get_static()); // A
Recommended tutorial: " PHP video tutorial》
The above is the detailed content of How to form static binding between static:: and new static() in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHPLate static binding: technical tips that break through traditional programming thinking. Introduction: The traditional way of programming thinking usually determines the calling of methods and properties at compile time. However, in some cases where dynamic calling is required, this method seems restrictive and unreasonable. Flexible. PHP provides a powerful feature, "Late static binding", which breaks traditional programming thinking and provides convenience for dynamic binding of methods and properties. This article introduces L through specific code examples.

Yes! When the compiler knows the object to be used for method execution, it can statically bind the reference to the object. For example, static variables, private variables and final variables use static binding. If object recognition is required at runtime, use dynamic binding. Method overriding is a case of dynamic binding. Method overloading is a case of static binding.

How to use PHPLate static binding to improve code reusability Introduction: In PHP development, code reuse is one of the key factors to improve development efficiency and maintainability. PHP provides a variety of techniques to achieve code reusability, one of the important techniques is the use of Late static binding. This article will introduce the concept, advantages and application of Late static binding in actual development. 1. Overview of Late static binding Late static binding refers to the method of dynamically determining the calling method of static methods or properties based on the context when calling. exist

Create an efficient and scalable code structure through PHPLate static binding Summary: In large projects, the scalability and efficiency of the code structure are very important. PHPLate static binding is a powerful feature that helps us build code that is easy to maintain and extend. This article will introduce the concept of PHPLate static binding and use specific code examples to illustrate how to use it to build efficient and scalable code structures. Introduction: As the company's business continues to develop and expand, the project scale also gradually increases. In this way

Unlocking the technical secrets of PHPLate static binding requires specific code examples. In recent years, PHP, as a commonly used server-side scripting language, has been welcomed by developers. With the development of the PHP language, more and more programming techniques and features have been added to PHP, one of which is Late static binding. Late static binding means that in an inheritance relationship, subclasses can override and call static methods and properties of the parent class. This inheritance relationship can greatly improve the flexibility and scalability of the code. So let me

Binding is a mechanism that creates a link between a method call and the actual implementation of the method. According to the concept of polymorphism in Java, objects can have many different forms. The object form can be resolved at compile time and run time. If the link between method invocation and method implementation is resolved at compile time, we call it static binding; if it is resolved at runtime, we call it dynamic binding. Dynamic binding uses objects to resolve bindings, while static binding uses classes and fields' types. Old gentleman. no. Key static binding Dynamic binding 1 Basic resolution at compile time Resolved at run time 2 Resolution mechanism Static binding uses the type of class and field Dynamic binding uses objects to resolve binding 3 Example overloading is an example of static binding Method overloading Writing is an example of dynamic binding 4. Method type is private,

PHPLate static binding: a technical tool for optimizing code inheritance Background introduction: In object-oriented programming, inheritance is a common code reuse technology. Through inheritance, we can create a new class and inherit properties and methods from an existing class (called parent or base class). In this way, we can reduce code duplication and improve code maintainability and scalability. However, in inheritance, we often encounter a problem: when calling the static method of the parent class in the subclass, because the static method binding is completed at compile time, the subclass calls the static method.
