Usage of = > and – > and:: symbols in PHP

Time:2022-1-15

It doesn’t matter. Let’s explain in detail below. If you have the foundation of C + + and Perl, you will find that these guys have the same symbolic functions as some of them.

‘- >’ symbol

The infix dereference operator. In other words, it is a method that calls a subroutine whose parameters are passed by reference (and, of course, for other purposes). As we mentioned above, when calling PHP functions, most parameters are passed by reference. The ‘- >’ function in PHP is the same as they are in Perl or C + +. The following is a simple dereference example:

echo $x->def(); # output

‘= >’ operator

The ‘= >’ operator is very common in PHP scripts. Because PHP array functions are very rich, we often use arrays because it is very convenient to operate data.

  $phparr= new array( in => ‘reply,’

  side => ‘left’,

  padx => 2m,

  pady => 2m,

  ipadx => 2m,

  ipady => 1m

  )

By the way, if you need to use the number “greater than or equal to”, you should use “> =” instead of “= >”.

:: field operator

In PHP, “::” is called range resolution operator, also known as domain operator

The “::” symbol can be regarded as the same as the “.” in C language Similar, and it is more like the:: class range operator in C + + (Perl).

PHP calls the internal static members of a class, or calls between classes use::

Here is an example:

class A
{
static $count = 0;
static function haha()
{
//
}
function diaoyoug()
{
self::haha();
self::$count;
}
}

a.b.c; /* In C language*/

a::b::c(); // Functions in C + +

$a::b::c; # Scalars in Perl 5

“= = =” (equal sign)

Some people may doubt that this symbol not only judges whether the two variables are equal, but also judges whether the value types are consistent. If the value types are different, it will return false, such as: $a = “1”// Character type 1 $B = 1// Digital type 1 when executing $a = = = $B; False is returned when

“- >” (minus sign, right angle bracket)

Used in a class to access functions or objects in the class, such as:


<?php 
class Test { 
function do_test() {
 echo "Doing foo."; 
} } 
$bar = new Test; 
$bar->do_test(); 
?>

“= >” (equal sign, right angle bracket)

Assign values to the array, for example: $arr = array (“one” = > (“1” = > 10, “2” = > 20), “two” = > 2); Then $arr [“one”] [“1”] = 10;

Detailed explanation of the usage of = > and – > and:: in PHP

1. = > usage

In PHP, the default array key name is an integer, and you can also define any character key name yourself (preferably meaningful), such as:

$css=array(‘style’=>’0’,’color’=>’green’);

Then $CSS [‘style ‘] = =’0’, $CSS [‘color ‘] = =’green’.

2. – > usage

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

$arr=['a'=>123,'b'=>456];// Array initialization
echo $arr['a'];// Array reference
print_ r($arr);// View array
class A{
    public $a=123;
    public $b=456;
}
$obj=new A();
echo $obj->a;// object reference
print_ r($obj);// View object
?>

Output results:

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

3. :: usage of

The double colon operator, the scope resolution operator, can access properties and methods overridden in static, const, and classes.

(1) Program list: use variables to define external access in the class


class Fruit {
    const CONST_VALUE = 'Fruit Color';
}
$classname = 'Fruit';
echo $classname::CONST_VALUE; // As of PHP 5.3.0
echo Fruit::CONST_VALUE;
?>

(2) Program list: used outside the class definition:


    
  <?php 
  class Fruit { 
      const CONST_VALUE = 'Fruit Color'; 
  } 
   
  class Apple extends Fruit 
  { 
      public static $color = 'Red';
  
      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'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 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: method of calling base class


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

This is the end of this article on the usage of = > and – > and:: symbols in PHP. For more relevant PHP = > – >:: please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!