Static variables and methods

Source: Internet
Author: User

  This article mainly introduces the static variables and methods, the need for friends can refer to the

The static keyword declares that a property or method is related to a class, not a particular instance of a class, and therefore such a property or method is also called a class property or a class method.   If the access Control permission allows, you do not need to create the class object and use the class name plus two colons "::" directly. The   static keyword can be used to modify variables, methods.   without being instantiated, you can directly access static properties and static methods in a class.   Static properties and methods that can access only static properties and methods, and cannot have classes that access non-static properties and methods. Because static properties and methods are created, there may not be any instances of this class that can be invoked.   Static properties, only one in memory, shared for all instances.   Use self:: keyword to access static members of the current class. Static properties Common attributes   All instances of a class that share static properties in a class.   that is, even if there are multiple instances in memory, static properties are only one.   The following example sets a counter $count property, setting private and static adornments. In this way, the $count property is not directly accessible to the outside world. As a result of the program running, we also see that multiple instances are using the same static $count property.   Code is as follows: Class user{    private static $count = 0;//log login for all users.     Public Function __construct () {  & nbsp     Self:: $count = self:: $count + 1;    }     Public function GetCount () {            return self:: $count; nbsp  }     Public function __destruct () {        self:: $count = self:: $count-1;   &N Bsp } $user 1 = new user (); $usEr2 = new User (); $user 3 = new User (); echo "Now here have". $user 1->getcount (). "User"; echo "<br>"; Unset ($user 3); echo "Now-here have". $user 1->getcount (). "User";?>      Program Run results: 1 2   now have 3 user now Here have 2 user jb51.net static properties direct invocation   Static properties can be used directly without instantiating, and can be used directly when the class is not yet created.   is using the class name:: Static property name. The code is as follows: Class math{    public static $PI = 3.14  }//Find the area of a 3-radius garden. $r = 3; echo "radius is $r area is <br>"; echo Math:: $pi * $r * $R;   echo "<br><br>"; Here I think 3.14 is not accurate enough, I set it more accurate. Math:: $pi = 3.141592653589793; echo "radius is $r area is <br>"; echo Math:: $pi * $r * $r?>     program Run results: 1 2 3 4   RADIUS is 3 area is 28.26 radius is 28.2743338823   class no Created, static properties can be used directly. When is the static property created in memory? There is no relevant information in PHP. Refer to the concepts in Java to explain that they should also be generic.   Static properties and methods that are created when a class is invoked. Class is called, meaning that the class is created or any static members in the class are invoked. Static methods   Static methods do not require that the class being instantiated can be used directly.   is using the class name:: Static method name.   Let's continue to write this math class for mathematical calculations. We designed a method to calculate the maximum value. Since it's a math operation, we don't have to.To instantiate this class, it is much more convenient to use this method if it can be taken over.   We are just this class designed to demonstrate the static method. The max () function comparison value is provided in PHP.     Code is as follows: Class math{      public static function Max ($num 1, $num 2) {        $num 1 > $n Um2? $num 1: $num 2;    }    } $a = 99; $b = 88; echo "shows the maximum value in $ A and $ B is"; echo "<br>"; Echo Math::max ($a, $b); echo "<br>"; echo "<br>"; echo "<br>"; $a = 99; $b = 100; echo "shows the maximum value in $ A and $ B is"; echo "<br>"; Echo Math::max ($a, $b);?>      program Run results:   shows the maximum value in $ A and $ B is 99 display the maximum value in $ A and $ B is 100 static method how to invoke static side Method   The first example, when a static method invokes another static method, the class name is used directly.     Code is as follows: The math class that implements the maximum comparison. Class math{      public static function Max ($num 1, $num 2) {        $num 1 > $n Um2? $num 1: $num 2;    }     public static function Max3 ($num 1, $num 2, $num 3) {        $num 1 = Math::max ($num 1, $num 2);         $num 2 = Math::max ($num 2, $num 3);         $num 1 = Math::max ($num 1, $num 2);               return $num 1;    }} $a = 99; $b = 77; $c = 88; The maximum value of the echo "show $a   $b $c   is"; echo "<br>"; Echo math::max3 ($a, $b, $c);?>      Program Run results: 1 2   display 99 77 88 The maximum value is   You can also use self:: Invoke the other in the current class static methods. (suggested)   code as follows: The math class that implements the maximum comparison. Class math{      public static function Max ($num 1, $num 2) {        $num 1 > $n Um2? $num 1: $num 2;    }     public static function Max3 ($num 1, $num 2, $num 3) {        $num 1 = Self::max ($num 1, $num 2);         $num 2 = Self::max ($num 2, $num 3);         $num 1 = Self::max ($num 1, $num 2);               return $num 1;    }} $a = 99; $b = 77; $c = 88; The maximum value of the echo "show $a   $b $c   is"; echo "<br>"; Echo math::max3 ($a, $b, $c);?>      Program Run results: 1 2   display 99 77 88 The maximum value is 99 static method call static property   Use class name:: Static Genus The property name calls the static properties in this class.   Code is as follows: Class circle{    public static $PI = 3.14       public static function Circleacreage ($r) {&N Bsp     Return $r * $r * Circle:: $PI;    }} $r = 3; The area of the echo "Radius $r circle is". Circle::circleacreage ($r);?>      Program Run Results: 1   RADIUS 3 of the Circle area is 28.26   Use self:: Invoke the static properties of this class. (suggested)   code as follows: Class circle{    public static $PI = 3.14       public static function Circleacreage ($r) {&N Bsp     Return $r * $r * self:: $PI;    }} $r = 3; The area of the echo "Radius $r circle is". Circle::circleacreage ($r);?>      program run Result: 1   RADIUS 3 Circle area is 28.26 static method cannot invoke Non-static property   static method cannot call Non-static genus Of Cannot use Self:: Invoke Non-static property.     Code is as follows: Class circle{    Public $pi = 3.14       public static functionCircleacreage ($r) {      return $r * $r * self::p i;    }} $r = 3; The area of the echo "Radius $r circle is". Circle::circleacreage ($r);?>      Program Run results: 1   Fatal error:undefined class constant ' pi ' in e:phpp Rojectstest.php on line 7   also cannot use $this to get values for non-static properties.   Code is as follows: Class circle{    Public $pi = 3.14       public static function Circleacreage ($r) {  &N Bsp   Return $r * $r * $this->pi;    }} $r = 3; The area of the echo "Radius $r circle is". Circle::circleacreage ($r);?>     Program Run results: 1   Fatal error:using $this when isn't in object context in E:p hpprojectstest.php on line 7 static method calls Non-static methods   PHP5, a non-static method cannot be invoked in a static method using the $this identity.   Code is as follows: The math class that implements the maximum comparison. Class math{       Public Function Max ($num 1, $num 2) {       -echo "bad<br>";                return $num 1 > $num 2? $num 1: $num 2;   &NBSP }     public static function Max3 ($num 1, $num 2, $num 3) {        $num 1 = $this->max ($num 1, $nu m2);         $num 2 = $this->max ($num 2, $num 3);         $num 1 = $this->max ($num 1, $num 2);               return $num 1;    }} $a = 99; $b = 77; $c = 188; The maximum value of the echo "show $a   $b $c   is"; echo "<br>"; Echo math::max3 ($a, $b, $c);?>      program Run results:   shows the maximum value of 99 77 188 is Fatal error:using $this when not In the object context in e:test.php the line   when a non-static method in a class is self:: called, the system automatically converts this method to a static method.   The following code was executed, and there was a result. Because the Max method is converted to a static method by the system.     Copy code code as follows: The math class that implements the maximum comparison. Class math{       Public Function Max ($num 1, $num 2) {              Return $num 1 > $num 2? $num 1: $num 2;    }     public static function Max3 ($num 1, $num 2, $num 3) {        $num 1 = Self::max ($num 1, $num 2);         $num 2 = Self::max ($num 2, $num 3);         $num 1 = Self::max ($num 1, $num 2);               return $num 1;    }} $a = 99; $b = 77; $c = 188; The maximum value of the echo "show $a   $b $c   is"; echo "<br>"; Echo math::max3 ($a, $b, $c);?>      Program Run results: 1 2   shows the maximum value in 99 77 188 is 188   in the following example, we let the static method Max3 Self:: Called Non-static method Max, has let Non-static method Max call Non-static property $pi by $this.   In the running is the wrong, this error and the previous example 3-1-9.php, this time is not static method Max reported a static method call Non-static properties of the error.   This proves a point where the Non-static method we defined here is automatically converted to a static method by the system.   Code is as follows: The math class that implements the maximum comparison. Class math{    Public $pi = 3.14       Public Function Max ($num 1, $num 2) {      & nbsp echo self:: $PI;  //The call here doesn't seem to be a problem.         return $num 1 > $num 2? $num 1: $num 2;    }     public static function Max3 ($num 1, $num 2, $num 3) {        $NUM1 = Self::max ($num 1, $num 2);         $num 2 = Self::max ($num 2, $num 3);         $num 1 = Self::max ($num 1, $num 2);               return $num 1;    }} $a = 99; $b = 77; $c = 188; The maximum value of the echo "show $a   $b $c   is"; echo "<br>"; Echo math::max3 ($a, $b, $c);?>      Program Run results: 1 2   shows the maximum value in 99 77 188 is Fatal error:access to Undecla Red static Property:math:: $pi in e:phpprojectstest.php on line 7  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.