The use of static property static methods in PHP

Source: Internet
Author: User

The static keyword is used to modify properties, methods, which are called properties, methods as static properties, and static methods. The static keyword declares that a property or method is related to a class and not to a particular instance of a class, so such a property or method is also referred to as a class property or class method. If the access Control permission allows, you do not have to create the class object and directly use the class name plus two colons "::" Call. The static keyword can be used to modify variables, methods. You can directly access static properties and static methods in a class without having to be instantiated. Static properties and methods can only access static properties and methods, and non-static properties and methods cannot be accessed by class. Because static properties and methods are created, there may not be any instances of this class that can be called. Static property, in memory, only one copy, using the self:: keyword to access the current class of a statically member or method.    <?php//non-static method call static property class general_call_static_property{private static $count = 0; Public Function __construct () {self:: $count =self:: $count +1;//non-static methods can use static properties without instantiation, and can be used directly when the class has not been created.    Used by self:: static property name} public function print_content () {return self:: $count;        } public Function __destruct () {self:: $count =self:: $count-1; }} $obj 1=new General_call_static_property (); $obj 2=new General_call_static_property (); $obj 3=new general_call_static _property (); Echo $obj 1->print_content (). ' <br/> ';//3echo $obj 2->print_content (). ' <br/> ';//3unset ($obj 2); Echo $obj 3->print_content (). ' <br/> ';//2//non-static method call static method Class general_call_static_method{public Static function Compare ($num 1, $num 2) {return $num 1> $num 2? $num 1: $num 2; The Public function result ($num 1, $num 2, $num 3) {$first =self::compare ($num 1, $num 2);//non-static methods can use static properties without instantiation, using the SEL        F:: Static method Name $second =self::compare ($num 2, $num 3);        $max =self::compare ($FIRSRT, $second);    return $max; }} $show =new General_call_static_method (); $a =33; $b =16; $c =70;echo ' show $ A, $b, $c Maximum: '. $show->result ($a, $b, $c). ' <br/> ';//70//call static property directly class call_property{public static $p = 3.14;} $r =5;echo ' radius of the circle area of R: '. Call_property:: $p * $r * $r. ' <br/> '; 78.5 static properties can be used directly without instantiation and can be used directly when the class is not created. Usage is class Name:: Static property name//Direct call static method class Call_method{public static function Max ($num 1, $num 2) {return $num 1> $num 2? $num 1    : $num 2; }}echo ' compares the maximum value of two numbers: '. Call_method::max (49,23). ' <br/> ';//49 static methods do not need to be instantiated in the same class to be used directly. Usage is class Name:: Static method name $a=6; $b =14;echo ' compare $ A and $b size: '. Call_method::max ($a, $b). ' <br/> ';//14//static method call static method class call_static_method{Private static $property = 10;  public static function count () {echo self:: $property =self:: $property +1;//static method call static variable is used by self:: Static variable name} public    static function result () {return self::count (); static method call static method is used by self:: Static method Name}}call_static_method::result ();//11?>

  

The use of static property static methods in PHP

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.