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=['a'=>123,'b'=>456];//数组初始化 echo $arr['a'];//数组引用 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 = 'Fruit Color'; } $classname = 'Fruit'; 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 = '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 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!