| PHP | usage of => and-> and:, and php
1. => usage
In php, the default key name of the array is an integer, or you can define any character key name (preferably practical), for example:
$ Css = array ('style' => '0', 'color' => 'green ');
Then $ css ['style'] = '0', $ css ['color'] = 'green '.
2.-> usage
-> Used to reference object members (attributes and methods );
1 <? Php 2 $ arr = ['A' => 123, 'B' => 456]; // array initialization 3 echo $ arr ['a']; // array reference 4 print_r ($ arr); // view array 5 class A {6 public $ a = 123; 7 public $ B = 456; 8} 9 $ obj = new A (); 10 echo $ obj-> a; // The object references 11 print_r ($ obj); // view the object 12?>
Output result:
123Array( [a] => 123 [b] => 456)123A Object( [a] => 123 [b] => 456)
3. Usage:
The dual-colon Operator is the Scope limitation Operator. Scope Resolution Operator can access static, const, and attributes and methods that are rewritten in the class.
(1) Program List: Use variables to define external access in the class
1 <?php 2 class Fruit { 3 const CONST_VALUE = 'Fruit Color'; 4 } 5 6 $classname = 'Fruit'; 7 echo $classname::CONST_VALUE; // As of PHP 5.3.0 8 9 echo Fruit::CONST_VALUE;10 ?>
(2) Program List: used outside the class definition ::
1 2 <?php 3 class Fruit { 4 const CONST_VALUE = 'Fruit Color'; 5 } 6 7 class Apple extends Fruit 8 { 9 public static $color = 'Red';10 11 public static function doubleColon() {12 echo parent::CONST_VALUE . "\n";13 echo self::$color . "\n";14 }15 }16 17 Apple::doubleColon();18 ?>
(3) Program List: Call the parent Method
1 <?php 2 class Fruit 3 { 4 protected function showColor() { 5 echo "Fruit::showColor()\n"; 6 } 7 } 8 9 class Apple extends Fruit10 {11 // Override parent's definition12 public function showColor()13 {14 // But still call the parent function15 parent::showColor();16 echo "Apple::showColor()\n";17 }18 }19 20 $apple = new Apple();21 $apple->showColor();22 ?>
(4) Program List: Use the scope qualifier
1 2 <?php 3 class Apple 4 { 5 public function showColor() 6 { 7 return $this->color; 8 } 9 }10 11 class Banana12 {13 public $color;14 15 public function __construct()16 {17 $this->color = "Banana is yellow";18 }19 20 public function GetColor()21 {22 return Apple::showColor();23 }24 }25 26 $banana = new Banana;27 echo $banana->GetColor();28 ?>
(5) Program List: Call the method of the base class
1 2 <?php 3 4 class Fruit 5 { 6 static function color() 7 { 8 return "color"; 9 }10 11 static function showColor()12 {13 echo "show " . self::color();14 }15 }16 17 class Apple extends Fruit18 {19 static function color()20 {21 return "red";22 }23 }24 25 Apple::showColor();26 // output is "show color"!27 28 ?>