Static variable index of PHP class

WBOY
Release: 2016-08-08 09:33:10
Original
1026 people have browsed it

The use of static variables in PHP is relatively wide. Not only can we add the static modifier in front of a class, method or variable, we can even add the static keyword to the internal variables of the function. The value of a variable with the static modifier added will not be lost even after the function is executed. That is to say, the variable will still remember the original value the next time this function is called.

Directly upload the code

<?php
class example{
    public static $pa;
    public $pb;
    public function __construct(){
        $this->pb = ++self::$pa;
    }
}

$a = new example;
$b = new example;

echo $a->pb;
echo '<hr/>';
echo $b->pb;
?>
Copy after login

Originally I thought the result should be

<span>1</span> ----------------------------------------------------- <span>1</span>
Copy after login

But I was wrong, The correct result is

<span>1</span> ---------------------------------------------------------------------------------- <span>2</span>
Copy after login

If you haven’t learned the basics well, hurry up and catch up.

The above introduces the static variable index of the PHP class, including PHP static variables and index content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!