可能重复:
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