几个PHP的静态方法的程序例子_PHP教程

WBOY
Freigeben: 2016-07-13 10:33:53
Original
885 Leute haben es durchsucht

静态方法的规则和静态变量是相同的。使用ststic关键字可以将方法标识为静态方法,通过类的名称和作用域限定操作符::可以访问静态方法。

静态方法和非静态方法之间有一个很重要的区别,就是在调用静态方法时,我们不需要创建类的实例。

Program List:用类名作为参数

用类名作为参数可以解决非继承的静态问题。

  
<?php
class Fruit {
	public static $category = "I'm fruit";
    
    static function find($class) 
	{
    	$vars = get_class_vars($class) ;
      	echo $vars['category'] ;
    }
}
class Apple extends Fruit {
     public static $category = "I'm Apple";
}
Apple::find("Apple");
?>
Nach dem Login kopieren

程序运行结果:

I'm Apple
Nach dem Login kopieren

Program List:重写基类方法

在派生类重写基类的方法。

  
<?php
class Fruit
{
	static function Foo ( $class = __CLASS__ )
   	{
       	call_user_func(array($class, 'Color'));
   	}
}
class Apple extends Fruit
{
   	static function Foo ( $class = __CLASS__ )
   	{
       	parent::Foo($class);
   	}
   	static function Color()
   	{
       	echo "Apple's color is red";
   	}
}
Apple::Foo(); // This time it works.
?>
Nach dem Login kopieren

程序运行结果:

Apple's color is red
Nach dem Login kopieren

Program List:静态数组的使用

静态和const作用域都可以用::操作符访问,如果你想使用::操作符访问数组,你需要事先将数组声明为静态。

  
<?php
class Fruit
{
  	static $color = array('color1' => 'red', 'color2' => 'yellow');
}
class Apple
{
  	public function __construct()
  	{
    	var_dump(Fruit::$color);
  	}
}
class Banana
{
  public function __construct()
  {
    Fruit::$color = FALSE;
  }
}
new Apple();    // prints array(2) { ["color1"]=> string(3) "red" ["color2"]=> string(6) "yellow" } 
echo '<br />';
new Banana();
new Apple();    // prints bool(false)
?>
Nach dem Login kopieren

程序运行结果:

array(2) { ["color1"]=> string(3) "red" ["color2"]=> string(6) "yellow" } 
bool(false)
Nach dem Login kopieren

Program List:再来一个单例模式

Static真的很酷,下面的程序演示了如何获得一个已经存在的实例。

    
<?php
class Singleton {
    private static $instance=null;
    private $value=null;
    private function __construct($value) {
        $this->value = $value;
    }
    public static function getInstance() {
        if ( self::$instance == null ) {
            echo "<br>new<br>";
            self::$instance = new Singleton("values");
        }  
		else {
            echo "<br>old<br>";
        }
        return self::$instance;
    }
}
$x = Singleton::getInstance();
var_dump($x); // returns the new object
$y = Singleton::getInstance();
var_dump($y); // returns the existing object
?>
Nach dem Login kopieren

程序运行结果:

new
object(Singleton)#1 (1) { ["value:private"]=> string(6) "values" } 
old
object(Singleton)#1 (1) { ["value:private"]=> string(6) "values" }
Nach dem Login kopieren

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/752389.htmlTechArticle静态方法的规则和静态变量是相同的。使用ststic关键字可以将方法标识为静态方法,通过类的名称和作用域限定操作符::可以访问静态方法...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!