Home >Backend Development >PHP Tutorial >Several program examples of PHP static methods_PHP tutorial

Several program examples of PHP static methods_PHP tutorial

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

The rules for static methods are the same as static variables. Use the ststic keyword to identify a method as a static method, which can be accessed through the name of the class and the scope qualification operator::.

A very important difference between static methods and non-static methods is that when calling static methods, we do not need to create an instance of the class.

Program List: Use class name as parameter

Using class names as parameters can solve non-inherited static problems.

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

Program execution result:

I'm Apple

Program List: Overriding base class methods

Override the base class method in the derived class.

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

Program execution result:

Apple's color is red

Program List: Use of static arrays

Both static and const scopes can be accessed using the :: operator. If you want to use the :: operator to access an array, you need to declare the array as static in advance.

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

Program execution result:

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

Program List: Another singleton pattern

Static is really cool, the program below demonstrates how to get an already existing instance.

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

Program execution result:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752389.htmlTechArticleThe rules for static methods are the same as static variables. Use the ststic keyword to identify a method as a static method. Static methods can be accessed through the name of the class and the scope qualification operator::...
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