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