Heim  >  Artikel  >  Backend-Entwicklung  >  PHP作用域限定符::的几个程序例子_PHP教程

PHP作用域限定符::的几个程序例子_PHP教程

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

双冒号::被认为是作用域限定操作符,用来指定类中不同的作用域级别。::左边表示的是作用域,右边表示的是访问的成员。

系统定义了两个作用域,self和parent。self表示当前类的作用域,在类之外的代码是不能使用这个操作符的。

Program List:使用self作用域访问父类中的函数

  
<?php
	class NowaClass
	{
		function nowaMethod()
		{
			print '我在类 NowaClass 中声明了。';
		}
	}
	
	class ExtendNowaClass extends NowaClass
	{
		function extendNowaMethod()
		{
			print 'extendNowaMethod 这个方法在 ExtendNowaClass 这个类中声明了。';
			self::nowaMethod();
		}
	}
	
	ExtendNowaClass::extendNowaMethod();
	
?>

程序运行结果:

extendNowaMethod 这个方法在 ExtendNowaClass 这个类中声明了。
我在类 NowaClass 中声明了。

parent这个作用域很简单,就是派生类用来调用基类的成员时候使用。

Program List:使用parent作用域

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

程序运行结果:

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

Program List:用基类的方法访问派生类的静态成员

如何继承一个存储位置上的静态属性。

  
<?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();
?>

程序运行结果:

yellow
orange。

Program List:静态函数初始化

在一个类中初始化静态变量比较复杂,你可以通过创建一个静态函数创建一个静态的构造器,然后在类声明后马上调用它来实现初始化。

    
<?php
class Fruit 
{
    private static $color = "White";
    private static $weigth;
    public static function init() 
	{
        echo self::$weigth = self::$color . " Kilogram!";
    }
}
Fruit::init();
?>

程序运行结果:

White Kilogram!

Program List:一个简单的单例模式例子

这个应该可以帮到某些人吧。

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

程序运行结果:

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.htmlTechArticle双冒号::被认为是作用域限定操作符,用来指定类中不同的作用域级别。::左边表示的是作用域,右边表示的是访问的成员。 系统定义了两...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn