在php中數(shù)組默認(rèn)鍵名是整數(shù),也可以自己定義任意字符鍵名(最好是有實(shí)際意義),如:
$css=array('style'=>'0',‘color’=>‘green‘); 則$css['style']=='0',$css['color']=='green'。
2、->的用法
->用來(lái)引用對(duì)象的成員(屬性與方法);
1 <?php 2 $arr=['a'=>123,'b'=>456];//數(shù)組初始化 3 echo $arr['a'];//數(shù)組引用 4 print_r($arr);//查看數(shù)組 5 html' target='_blank'>class A{ 6 public $a=123; 7 public $b=456; 8 } 9 $obj=new A(); 10 echo $obj->a;//對(duì)象引用 11 print_r($obj);//查看對(duì)象 12 ?>
輸出結(jié)果:
123Array( [a] => 123 [b] => 456)123A Object( [a] => 123 [b] => 456)
3、::的用法
雙冒號(hào)操作符即作用域限定操作符Scope Resolution Operator可以訪(fǎng)問(wèn)靜態(tài)、const和類(lèi)中重寫(xiě)的屬性與方法。
(1)Program List:用變量在類(lèi)定義外部訪(fǎng)問(wèn)
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:在類(lèi)定義外部使用::
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:調(diào)用parent方法
1 <?php 2 class Fruit 3 { 4 protected function showColor() { 5 echo "Fruit::showColor()/n"; 6 } 7 } 8 9 class Apple extends Fruit 10 { 11 // Override parent's definition 12 public function showColor() 13 { 14 // But still call the parent function 15 parent::showColor(); 16 echo "Apple::showColor()/n"; 17 } 18 } 19 20 $apple = new Apple(); 21 $apple->showColor(); 22 ?>
(4)Program List:使用作用域限定符
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:調(diào)用基類(lèi)的方法
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 Fruit 18 { 19 static function color() 20 { 21 return "red"; 22 } 23 } 24 25 Apple::showColor(); 26 // output is "show color"! 27 28 ?>
以上就是PHP中=>和->以及::的用法的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注 其它相關(guān)文章!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注