可能重複:
Reference - What does this symbol mean in PHP?PHP:什麼是「 - >」和「=>」?
我已經和PHP編程了一點了,但每過一段時間我碰到這兩個表達式運行:
->
=>
這些是什麼以及它們的含義是什麼?我甚至不知道該怎麼稱呼它們才能找出...
可能重複:
Reference - What does this symbol mean in PHP?PHP:什麼是「 - >」和「=>」?
我已經和PHP編程了一點了,但每過一段時間我碰到這兩個表達式運行:
->
=>
這些是什麼以及它們的含義是什麼?我甚至不知道該怎麼稱呼它們才能找出...
->
是用於訪問對象的屬性和方法:
class myClass
{
public $anAttribute = 'hey this is my attribute';
public function myMethod()
{
return 'this is my method';
}
}
$class = new myClass();
echo $class->anAttribute;
echo $class->myMethod();
=>
在兩個地方被使用。這可以是同時在foreach
語句來手動或動態實例化的數組:
// Manually instantiated:
$myArray = new array('fruit' => 'apple', 'meat' => 'sausage');
echo $myArray['fruit'];
echo $myArray['meat'];
// Dynamic in foreach
foreach($myArray AS $key => $value)
{
echo "myArray['$key'] is $value";
}
->
用於訪問對象的實例屬性。它與許多其他語言(C,C++,Python,Javascript)中的.
語法等效。
$myclass->my_instance_var;
$myclass->my_instance_method();
=>
用於將鍵映射到關聯數組中的值。它在Python和Javascript中的映射中相當於:
。
$arr = Array("Hello" => "World", "Foo" => "Bar");
你有兩個例子嗎?我不明白爲什麼我需要使用它們。 – dcolumbus 2011-04-01 06:51:50
_associative arrays_這不是一個限制,您可以使用數字索引數組以及 – 2011-04-01 06:53:31
@Shakti當您指定數字索引時,那些是關聯數組。從技術上講,在PHP中,所有數組都是關聯的,但有一個特殊情況,其中爲您分配鍵。 @dcolumbus如果你編寫或使用類,你會使用第一個;如果你使用關聯數組,你會使用秒。 – 2011-04-01 06:54:33
->
此符號用於指對象
$obj->age=25;
$obj->setAge();
=>
該符號被用於在陣列
$array=array('age'=>25,'name'=>'test');
$array=array(10=>20, 30=>50);
所以在PHP中,我是否必須使用「 - >」才能調用類中的方法?在ActionScript中,我所要做的就是:obj.function(); – dcolumbus 2011-04-01 06:53:21
@dcolumbus:是的,但靜態方法在這種情況下是異常的,可以用'classname :: statcimethod()'調用它們。 – 2011-04-01 06:54:47
它們都是運營商分配的值的屬性或方法。
=>是數組的賦值運算符,將值賦給指定的鍵。 看一看http://www.php.net/manual/en/language.operators.assignment.php
的 - >是訪問,所以如果你有一類Foo具有可變酒吧,你會訪問使用 - >操作:
// Get value of Bar $value = $fooInstance->Bar
基本上 - >運算符類似於「。」在Java和C#
http://stackoverflow.com/questions/2588149/in-php-what-is-the-operator-called-and-你怎麼說,當你讀的代碼 – eisberg 2011-04-01 06:49:45
至於' - >',請參見[這是什麼意思在PHP中: - >](http://stackoverflow.com/questions/3910147/what-確實,這個均值功能於PHP)。 – jensgram 2011-04-01 06:49:50