Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How static members inherit in PHP

Shulou Source: shulou.com Published: 2022-06-02 06:52:41 10月20日 Update

This article focuses on "how static members inherit in PHP". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how static members inherit in PHP.

How do static members inherit in PHP?

Static members, that is, variables or methods modified with static, are prone to errors if they are not clear about how they are implemented. This time we are going to look at the invocation of static members in inheritance. First, let's look at a piece of code like this:

Class A

{

Static $a = 'This is Aids'

Public function show ()

{

Echo self::$a, PHP_EOL

Echo static::$a, PHP_EOL

}

}

Class B extends A

{

Static $a = 'This is financing'

}

B = new B

$b-> show ()

In the previous article, we talked about the problem of self. Self simply points to the current class. Note that it is a class, not an instantiated object. So the output above is:

This is A!

This is B!

Well, with this foundation, we know that static members are class-related, not object-related. Then the following code is easier to understand.

Class C

{

Static $c = 1

Public $d = 1

}

Class D extends C

{

Public function add ()

{

Self::$c++

$this- > dumped +

}

}

$D1 = new D ()

$D2 = new D ()

$D1-> add ()

Echo'cvana'. DRV, DRV. ', dvl'. $D1-> d. ';, PHP_EOL

$d2-> add ()

Echo'cvana'. DRV, DRV. ', dvl'. $D2-> d. ';, PHP_EOL

Can you deduce the output by reading the code directly? In fact, as long as you master the principles mentioned above, this code is easy to understand. $c is a static variable and $d is a normal variable. After operating through the add () method of the class instance, $c is shared no matter which instance object it is, because it is related to the class. While $d is a normal variable, its scope is limited to the current instance object. Therefore, the result of the output is:

C:2,d:2

C:3,d:2

Finally, let's review the self, parent, and static keywords.

Class E {

Public static function test () {

Echo "This is E test!"

}

}

Class F extends E {

Public static function t () {

Self::test ()

Parent::test ()

Static::test ()

}

Public static function test () {

Echo "This is F test!"

}

}

FRV (FRV)

What are the three results of t () output? For more information, please see Static in the previous article PHP.

Test code: https://github.com/zhangyue0503/dev-blog/blob/master/php/201912/source/%E9%9D%99%E6%80%81%E6%88%90%E5%91%98%E5%9C%A8PHP%E4%B8%AD%E6%98%AF%E6%80%8E%E4%B9%88%E7%BB%A7%E6%89%BF%E7%9A%84%EF%BC%9F.php

Reference document: https://www.php.net/manual/zh/language.oop5.static.php

At this point, I believe you have a deeper understanding of "how static members inherit in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Static member code variable instance object result output method general content article learning relevant practical deeper clear three above that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info NVidia MySQL Docker