Home  >  Article  >  Backend Development  >  Usage of => and -> and :: in PHP

Usage of => and -> and :: in PHP

一个新手
一个新手Original
2017-09-14 09:49:5465212browse
Recommended manual:php complete self-study manual

1. Usage of =>

Array default key in php The name is an integer, you can also define any character key name yourself (preferably with practical meaning), such as:

 $css=array('style'=>'0',‘color’=>‘green‘);
  则$css['style']=='0',$css['color']=='green'。

2, usage of ->

-> is used to reference objects Members (properties and methods);

  <?php 
    $arr=[&#39;a&#39;=>123,&#39;b&#39;=>456];//数组初始化 
    echo $arr[&#39;a&#39;];//数组引用 
    print_r($arr);//查看数组 
    class A{ 
      public $a=123; 
      public $b=456; 
    } 
    $obj=new A();
    echo $obj->a;//对象引用
    print_r($obj);//查看对象
  ?>

Output results:

123Array(
    [a] => 123
    [b] => 456)
123A Object(
    [a] => 123
    [b] => 456)

3. Usage of ::

Double colon operator is the scope limitation operator Scope Resolution Operator You can access static, const, and overridden properties and methods in classes.

(1) Program List: Use variables to access outside the class definition

  <?php 
  class Fruit { 
      const CONST_VALUE = &#39;Fruit Color&#39;; 
  } 
   
  $classname = &#39;Fruit&#39;; 
  echo $classname::CONST_VALUE; // As of PHP 5.3.0 
   
  echo Fruit::CONST_VALUE;
  ?>

(2) Program List: Use outside the class definition::

    
  <?php 
  class Fruit { 
      const CONST_VALUE = &#39;Fruit Color&#39;; 
  } 
   
  class Apple extends Fruit 
  { 
      public static $color = &#39;Red&#39;;
  
      public static function doubleColon() {
          echo parent::CONST_VALUE . "\n";
          echo self::$color . "\n";
      }
  }
  
  Apple::doubleColon();
  ?>

(3) Program List: Call the parent method

  <?php 
  class Fruit 
  { 
      protected function showColor() { 
          echo "Fruit::showColor()\n"; 
      } 
  } 
   
  class Apple extends Fruit
  {
      // Override parent&#39;s definition
      public function showColor()
      {
          // But still call the parent function
          parent::showColor();
          echo "Apple::showColor()\n";
      }
  }
  
  $apple = new Apple();
  $apple->showColor();
  ?>

(4) Program List: Use the scope qualifier

     
  <?php 
      class Apple 
      { 
          public function showColor() 
          { 
              return $this->color; 
          } 
      }
  
      class Banana12     {
          public $color;
  
          public function __construct()
          {
              $this->color = "Banana is yellow";
          }
  
          public function GetColor()
          {
              return Apple::showColor();
          }
      }
  
      $banana = new Banana;
      echo $banana->GetColor();
  ?>

(5) Program List: Call the base class method

       
  <?php 
   
  class Fruit 
  { 
      static function color() 
      { 
         return "color"; 
      }
  
      static function showColor()
      {
          echo "show " . self::color();
      }
  }
  
  class Apple extends Fruit
  {
      static function color()
      {
          return "red";
      }
  }
  
  Apple::showColor();
  // output is "show color"!
  
  ?>
Related article recommendations:
1.Introduction to the meaning of the double colon::range parsing operator in php
2.The difference between the double colon range parsing operator and the arrow-> operator in php
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial
##

The above is the detailed content of Usage of => and -> and :: in PHP. For more information, please follow other related articles on the PHP Chinese website!

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