PHP Scope Qualifier:: Several examples of programs _php tutorial

Source: Internet
Author: User
Double colon:: is considered a scope-qualified operator that is used to specify a different scope level in a class. :: The left represents the scope, and the right represents the access member.

The system defines two scopes, self and parent. Self represents the scope of the current class, and code outside the class cannot use this operator.

Program list: Using the self scope to access functions in the parent class

  
 
  

Program Run Result:

Extendnowamethod This method is declared in the Extendnowaclass class. I declare it in the class Nowaclass.

The parent scope is simple, and is used when derived classes are used to invoke members of the base class.

Program List: Using the parent scope

    
 
  Extendnowamethod ();? >

Program Run Result:

I am a function of a derived class. I am a function of the base class.

Program List: Accessing static members of derived classes using the method of a base class

How to inherit a static property on a storage location.

  
 
  Connect ();}    } Class Banana extends Fruit {    private static $bananaColor;    Public Function Connect () {        return self:: $bananaColor = ' yellow ';    }} Class Orange extends Fruit {    private static $orange _color;    Public Function Connect () {        return self:: $orange _color = ' orange ';    }} $banana = new Banana (); $orange = new Orange (); $banana->get (); $orange->get (); >

Program Run Result:

Yelloworange.

Program List: Static function initialization

Initializing a static variable in a class is complex, you can create a static constructor by creating a static function, and then invoke it immediately after the class declaration to initialize it.

    
 
  

Program Run Result:

White kilogram!

Program List: An example of a simple singleton pattern

This is supposed to help some people.

  
 
  color = ' Green ';    }    public static function getinstance ()    {      if (self:: $instance = = null)      {        print "Fruit object created!
"; Self:: $instance = new self; } Return self:: $instance; } Public Function Showcolor () { print ' My color is {$this-color}!
"; } Public Function SetColor ($color) { $this, color = $color; }} $apple = Fruit::getinstance (); Fruit Object created! $apple-Showcolor (); My color is green! $apple-SetColor ("Red"); $apple-Showcolor (); My color is red! $banana = Fruit::getinstance (); $banana-Showcolor (); My color is red! $banana-SetColor ("Yellow"); $apple-Showcolor (); My color is yellow!? >

Program Run Result:

Fruit Object created! My Color is green! My Color is red! My Color is red! My Color is yellow!

http://www.bkjia.com/PHPjc/752398.html www.bkjia.com true http://www.bkjia.com/PHPjc/752398.html techarticle Double colon:: is considered a scope-qualified operator that is used to specify a different scope level in a class. :: The left represents the scope, and the right represents the access member. The system defines two ...

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