Home Backend Development PHP Tutorial How to form static binding between static:: and new static() in PHP

How to form static binding between static:: and new static() in PHP

Jun 06, 2020 pm 03:20 PM
static binding

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 
}
Copy after login

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!
Copy after login
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!
Copy after login
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里调用
Copy after login

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
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

PHP Late static binding: technical tips to break through traditional programming thinking PHP Late static binding: technical tips to break through traditional programming thinking Sep 15, 2023 am 08:46 AM

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.

Static binding and dynamic binding in Java Static binding and dynamic binding in Java Aug 19, 2023 pm 02:57 PM

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 PHP Late static binding to improve code reusability How to use PHP Late static binding to improve code reusability Sep 15, 2023 am 11:36 AM

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 PHP Late static binding Create an efficient and scalable code structure through PHP Late static binding Sep 15, 2023 am 09:00 AM

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 PHP Late static binding Unlocking the technical secrets of PHP Late static binding Sep 15, 2023 am 09:39 AM

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

The difference between static binding and dynamic binding in Java The difference between static binding and dynamic binding in Java Aug 27, 2023 pm 11:09 PM

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,

PHP Late static binding: a technical tool for optimizing code inheritance PHP Late static binding: a technical tool for optimizing code inheritance Sep 15, 2023 am 08:57 AM

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.

See all articles