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

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

一个新手
Release: 2023-03-16 07:02:01
Original
65278 people have browsed it
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'。
Copy after login

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);//查看对象
  ?>
Copy after login

Output results:

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

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;
  ?>
Copy after login

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

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

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

(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"!
  
  ?>
Copy after login
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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template