A few examples of the static methods of PHP program _php Tutorial

Source: Internet
Author: User
Tags vars
The rules for static methods are the same as static variables. You can use the Ststic keyword to identify a method as a static method, using the name and scope of the class to qualify the operator:: You can access a static method.

One important difference between static and non-static methods is that when you call a static method, we don't need to create an instance of the class.

Program List: Using class-Named parameters

The non-inherited static problem can be solved by using the class famous as parameter.

  
 
  

Program Run Result:

I ' m Apple

Program List: Overriding base class methods

Overrides a method of a base class in a derived class.

  
 
  

Program Run Result:

Apple ' s color is red

Program List: Use of static arrays

Both static and const scopes can be accessed using:: Operators, and if you want to access an array using:: operator, you need to declare the array as static in advance.

  
 
  ' Red ', ' color2 ' = ' yellow ');} Class apple{public  function __construct ()  {    var_dump (Fruit:: $color);}  } Class banana{public  function __construct ()  {    Fruit:: $color = FALSE;  }} New Apple ();    Prints Array (2) {["Color1"]=> string (3) "Red" ["Color2"]=> string (6) "Yellow"} echo '
'; new Banana (); new Apple (); prints bool (false)?>

Program Run Result:

Array (2) {["Color1"]=> string (3) "Red" ["Color2"]=> string (6) "Yellow"} bool (FALSE)

Program list: One more singleton mode

Static is really cool, the following program shows how to get an already existing instance.

    
 
  Value = $value;    }    public static function getinstance () {        if (self:: $instance = = null) {            echo]
New
"; Self:: $instance = new Singleton ("values"); } else { echo '
Old
"; } Return self:: $instance; }} $x = Singleton::getinstance (); Var_dump ($x); Returns the new object$y = Singleton::getinstance (); Var_dump ($y); Returns the existing object?>

Program Run Result:

NewObject (Singleton) #1 (1) {["Value:private"]=> string (6) "Values"} oldobject (Singleton) #1 (1) {["Value:private"]= > string (6) "Values"}

http://www.bkjia.com/PHPjc/752389.html www.bkjia.com true http://www.bkjia.com/PHPjc/752389.html techarticle the rules for static methods are the same as static variables. You can use the Ststic keyword to identify a method as a static method, using the name and scope of the class to qualify the operator:: You can access static methods ...

  • 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.