PHP's Object oriented journey: An in depth understanding of static variables and methods

  • 2020-12-07 03:59:26
  • OfStack

The static keyword states that an attribute or method is related to a class and not to a particular instance of the class, so such attributes or methods are also referred to as "class attributes" or "class methods".

If access control permission allows, you can use the class name with two colons "::" instead of creating the class object.

The static keyword can be used to modify variables and methods.

You can directly access the static properties and static methods in the class without instantiation.

static's properties and methods can only be accessed by static's properties and methods, and non-static properties and methods cannot be accessed by classes. Because static properties and methods are created, there may not be any instances of this class that can be called.

The static attribute, which has only one copy in memory, is shared by all instances.

Access the static members of the current class using the self:: keyword.
Static properties are common properties

All instances of 1 class share static properties in the class.

That is, even if there are multiple instances in memory, there is only one copy of the static attribute.

In the following example, a counter $count attribute is set, and private and static decorations are set. This way, the $count attribute is not directly accessible to the outside world. The result of running the program is that we see multiple instances using the same static $count attribute.


<?
class user{
    private static $count = 0 ; // Log in for all users .
    public function __construct(){
        self::$count = self::$count + 1;
    }
    public function getCount(){    
        return self::$count;
    }
    public function __destruct(){
        self::$count = self::$count -1;
    }
}
$user1 = new user();
$user2 = new user();
$user3 = new user();
echo "now here have ".$user1->getCount()." user";
echo "<br>";
unset( $user3);
echo "now here have ".$user1->getCount()." user";
?> 

Program operation Results:
1
2

now here have 3 user
now here have 2 user ofstack.com
Static properties are called directly

Static properties can be used directly without instantiation and when the class is not yet created.

The method used is the class name :: static attribute name.


<?
class Math{
    public static $pi = 3.14;

}
// o 1 A radius 3 The size of a garden. 
$r = 3;
echo " The radius is  $r  Is the area of the <br>";
echo Math::$pi * $r * $r ;

echo "<br><br>";
// Here I think  3.14  It's not precise enough. I'm going to make it more precise. 
Math::$pi = 3.141592653589793;
echo " The radius is  $r  Is the area of the <br>";
echo Math::$pi * $r * $r ;
?>

Program operation Results:
1
2
3
4

The radius is 3 and the area is 3
28.26
The radius is 3 and the area is 3
28.2743338823

Class is not created, static properties can be used directly. So when are static properties created in memory? No relevant information is found in PHP. It should also be universal to refer to the concepts in Java to explain.

Static properties and methods that are created when the class is called. When a class is called, it means that the class is created or any static member of the class is called.
A static method

Static methods can be used without requiring the class to be instantiated.

The method used is the class name: : static method name.

Let's go ahead and write the Math class for the math. Let's design a method to figure out the maximum value. Since this is a mathematical operation, there is no need to instantiate the class, it would be much easier to use the method if you could just grab it.

We have designed this class only to demonstrate the static method. Comparison values of the max() function are provided in PHP.


<?
class Math{

    public static function Max($num1,$num2){
        return $num1 > $num2 ? $num1 : $num2;
    }    
}
$a = 99;
$b = 88;
echo " According to  $ a  and  $ b  The maximum value of ";
echo "<br>";
echo Math::Max($a,$b);
echo "<br>";echo "<br>";echo "<br>";
$a = 99;
$b = 100;
echo " According to  $ a  and  $ b  The maximum value of ";
echo "<br>";
echo Math::Max($a,$b);
?> 

Program operation Results:

Displays the maximum value of $a and $b
99
Displays the maximum value of $a and $b
100
How do static methods call static methods

For example 1, when a static method calls another static method, use the class name directly.


<?
//  To achieve maximum comparison Math Class. 
class Math{

    public static function Max($num1,$num2){
        return $num1 > $num2 ? $num1 : $num2;
    }
    public static function Max3($num1,$num2,$num3){
       $num1 = Math::Max($num1,$num2);
       $num2 = Math::Max($num2,$num3);
       $num1 = Math::Max($num1,$num2);       
       return $num1;
    }
}
$a = 99;
$b = 77;
$c = 88;
echo " According to  $a  $b $c   The maximum value of ";
echo "<br>";
echo Math::Max3($a,$b,$c);
?> 

Program operation Results:
1
2

It shows that the maximum of 99, 77, 88 is
99

You can also use self:: to call other static methods in the current class. (recommended)


<?
//  To achieve maximum comparison Math Class. 
class Math{

    public static function Max($num1,$num2){
        return $num1 > $num2 ? $num1 : $num2;
    }
    public static function Max3($num1,$num2,$num3){
       $num1 = self::Max($num1,$num2);
       $num2 = self::Max($num2,$num3);
       $num1 = self::Max($num1,$num2);       
       return $num1;
    }
}
$a = 99;
$b = 77;
$c = 88;
echo " According to  $a  $b $c   The maximum value of ";
echo "<br>";
echo Math::Max3($a,$b,$c);
?> 

Program operation Results:
1
2

It shows that the maximum of 99, 77, 88 is
99
Static methods call static properties

The static property in this class is invoked using the class name :: static property name.


<?
//
class Circle{
    public static $pi = 3.14;

    public static function circleAcreage($r){
      return $r * $r * Circle::$pi;
    }
}
$r = 3;
echo "  The radius of  $r  The area of the circle of theta is theta  " . Circle::circleAcreage($r);
?> 

Program operation Results:
1

The area of a circle with radius 3 is 28.26

Call the static property of this class with self::. (recommended)


<?
//
class Circle{
    public static $pi = 3.14;

    public static function circleAcreage($r){
      return $r * $r * self::$pi;
    }
}
$r = 3;
echo "  The radius of  $r  The area of the circle of theta is theta  " . Circle::circleAcreage($r);
?> 

Program operation Results:
1

The area of a circle with radius 3 is 28.26
Static methods cannot call non-static properties

Static methods cannot call non-static properties. You cannot call non-static properties using self::.


<?
//
class Circle{
    public $pi = 3.14;

    public static function circleAcreage($r){
      return $r * $r * self::pi;
    }
}
$r = 3;
echo "  The radius of  $r  The area of the circle of theta is theta  " . Circle::circleAcreage($r);
?> 

Program operation Results:
1

Fatal error: Undefined class constant 'pi' in E:PHPProjectstest.php on line 7

You cannot use $this to get the value of a non-static attribute.


<?
//
class Circle{
    public $pi = 3.14;

    public static function circleAcreage($r){
      return $r * $r * $this->pi;
    }
}
$r = 3;
echo "  The radius of  $r  The area of the circle of theta is theta  " . Circle::circleAcreage($r);
?>

Program operation Results:
1

Fatal error: Using $this when not in object context in E:PHPProjectstest.php on line 7
Static methods call non-static methods

In PHP5, you cannot call a non-static method in a static method using the $this identity.


<?
//  To achieve maximum comparison Math Class. 
class Math{   
    public function Max($num1,$num2){
        echo "bad<br>";       
        return $num1 > $num2 ? $num1 : $num2;
    }
    public static function Max3($num1,$num2,$num3){
       $num1 = $this->Max($num1,$num2);
       $num2 = $this->Max($num2,$num3);
       $num1 = $this->Max($num1,$num2);       
       return $num1;
    }
}
$a = 99;
$b = 77;
$c = 188;
echo " According to  $a  $b $c   The maximum value of ";
echo "<br>";
echo Math::Max3($a,$b,$c);
?> 

Program operation Results:

It shows that the maximum of 99, 77, 188 is
Fatal error: Using $this when not in object context in E:test.php on line 10

When a non-static method in a class is called by self::, the method is automatically converted to a static method.

The following code is executed and has a result. Because the Max method is converted to a static method by the system.


<?
class Math{
    public static $pi = 3.14;

}
// o 1 A radius 3 The size of a garden. 
$r = 3;
echo " The radius is  $r  Is the area of the <br>";
echo Math::$pi * $r * $r ;

echo "<br><br>";
// Here I think  3.14  It's not precise enough. I'm going to make it more precise. 
Math::$pi = 3.141592653589793;
echo " The radius is  $r  Is the area of the <br>";
echo Math::$pi * $r * $r ;
?>
0

Program operation Results:
1
2

It shows that the maximum of 99, 77, 188 is
188

In the following example, we had the static method Max3 use self:: to call the non-static method Max, and had the non-static method Max call the non-static property $pi via $this.

Like the previous example, 3-1-9.es231EN1, the non-static method Max reported an error when a static method called a non-static property.

This proves the point that the non-static method Max defined here is automatically converted to a static method by the system.


<?
//  To achieve maximum comparison Math Class. 
class Math{
    public $pi = 3.14;

    public function Max($num1,$num2){
        echo self::$pi;  // The call here shouldn't seem to be a problem .
        return $num1 > $num2 ? $num1 : $num2;
    }
    public static function Max3($num1,$num2,$num3){
       $num1 = self::Max($num1,$num2);
       $num2 = self::Max($num2,$num3);
       $num1 = self::Max($num1,$num2);       
       return $num1;
    }
}
$a = 99;
$b = 77;
$c = 188;
echo " According to  $a  $b $c   The maximum value of ";
echo "<br>";
echo Math::Max3($a,$b,$c);
?> 

Program operation Results:
1
2

It shows that the maximum of 99, 77, 188 is
Fatal error: Access to undeclared static property: Math::$pi in E:PHPProjectstest.php on line 7


Related articles: