Home  >  Article  >  Backend Development  >  Several program examples of PHP scope qualifier::_PHP tutorial

Several program examples of PHP scope qualifier::_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:53848browse

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.

Program List: Use self scope to access functions in the parent class

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

Program List: Use parent scope

    
<?php
	class NowaClass
	{
		function nowaMethod()
		{
			print '我是基类的函数。';
		}
	}
	
	class ExtendNowaClass extends NowaClass
	{
		function extendNowaMethod()
		{
			print '我是派生类的函数。';
			parent::nowaMethod();
		}
	}
	
	$obj = new ExtendNowaClass();
	$obj -> extendNowaMethod();
	
?>

Program execution result:

我是派生类的函数。
我是基类的函数。

Program List: Use base class methods to access static members of derived classes

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。

Program List: Static function initialization

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!

Program List: A simple example of singleton pattern

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!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752398.htmlTechArticleDouble colon:: is considered a scope qualification 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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn