Home >Backend Development >PHP Tutorial >Several program examples of PHP scope qualifier::_PHP tutorial
Double colon:: is considered a scoping operator and is used to specify different scope levels in a class. ::The left side represents the scope, and the right side represents the accessed members.
The system defines two scopes, self and parent. self represents the scope of the current class. Code outside the class cannot use this operator.
<?php class NowaClass { function nowaMethod() { print '我在类 NowaClass 中声明了。'; } } class ExtendNowaClass extends NowaClass { function extendNowaMethod() { print 'extendNowaMethod 这个方法在 ExtendNowaClass 这个类中声明了。'; self::nowaMethod(); } } ExtendNowaClass::extendNowaMethod(); ?>
Program execution result:
extendNowaMethod 这个方法在 ExtendNowaClass 这个类中声明了。 我在类 NowaClass 中声明了。
The scope of parent is very simple. It is used when a derived class calls members of a base class.
<?php class NowaClass { function nowaMethod() { print '我是基类的函数。'; } } class ExtendNowaClass extends NowaClass { function extendNowaMethod() { print '我是派生类的函数。'; parent::nowaMethod(); } } $obj = new ExtendNowaClass(); $obj -> extendNowaMethod(); ?>
Program execution result:
我是派生类的函数。 我是基类的函数。
How to inherit static properties on a storage location.
<?php class Fruit { public function get() { echo $this->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 execution result:
yellow orange。
Initializing static variables in a class is more complicated. You can create a static constructor by creating a static function, and then call it immediately after the class is declared to achieve initialization.
<?php class Fruit { private static $color = "White"; private static $weigth; public static function init() { echo self::$weigth = self::$color . " Kilogram!"; } } Fruit::init(); ?>
Program execution result:
White Kilogram!
This may help some people.
<?php class Fruit { private static $instance = null; private function __construct() { $this-> color = 'Green'; } public static function getInstance() { if(self::$instance == null) { print "Fruit object created!<br />"; self::$instance = new self; } return self::$instance; } public function showColor() { print "My color is {$this-> color}!<br>"; } 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 execution result:
Fruit object created! My color is Green! My color is Red! My color is Red! My color is Yellow!