什么';s:(双冒号)和->;(箭头)在PHP中?
在PHP中有两种不同的方法访问方法,但是有什么区别呢什么';s:(双冒号)和->;(箭头)在PHP中?,php,Php,在PHP中有两种不同的方法访问方法,但是有什么区别呢 $response->setParameter('foo', 'bar'); 及 我假设变量的函数使用->(带大于号或V字形的破折号),类的函数使用::(双冒号)。对吗 =>赋值运算符是否仅用于分配数组中的数据?这是否与用于实例化或修改变量的赋值运算符相比?如果左侧部分是对象实例,则使用->。否则,使用: 这意味着->主要用于访问实例成员(尽管它也可以用于访问静态成员,但不鼓励这样的使用),而::通常用于访问静态成员(尽管在一些特殊情
$response->setParameter('foo', 'bar');
及
我假设变量的函数使用->
(带大于号或V字形的破折号),类的函数使用::
(双冒号)。对吗
=>
赋值运算符是否仅用于分配数组中的数据?这是否与用于实例化或修改变量的赋值运算符相比?如果左侧部分是对象实例,则使用->
。否则,使用:
这意味着
->
主要用于访问实例成员(尽管它也可以用于访问静态成员,但不鼓励这样的使用),而::
通常用于访问静态成员(尽管在一些特殊情况下,它用于访问实例成员)
通常,::
用于,它可能有一个类名,父类
,自身
,或者(在PHP5.3中)静态
parent
指使用它的类的超类的范围self
指使用它的类的范围<代码>静态指的是“被调用的范围”(请参阅)
规则是使用:
的调用是实例调用,当且仅当:
- 目标方法未声明为静态和静态
- 调用时存在兼容的对象上下文,这意味着这些上下文必须为true:
- 调用是在
存在且$this
的类是被调用方法的类或其子类$this
class A {
public function func_instance() {
echo "in ", __METHOD__, "\n";
}
public function callDynamic() {
echo "in ", __METHOD__, "\n";
B::dyn();
}
}
class B extends A {
public static $prop_static = 'B::$prop_static value';
public $prop_instance = 'B::$prop_instance value';
public function func_instance() {
echo "in ", __METHOD__, "\n";
/* this is one exception where :: is required to access an
* instance member.
* The super implementation of func_instance is being
* accessed here */
parent::func_instance();
A::func_instance(); //same as the statement above
}
public static function func_static() {
echo "in ", __METHOD__, "\n";
}
public function __call($name, $arguments) {
echo "in dynamic $name (__call)", "\n";
}
public static function __callStatic($name, $arguments) {
echo "in dynamic $name (__callStatic)", "\n";
}
}
echo 'B::$prop_static: ', B::$prop_static, "\n";
echo 'B::func_static(): ', B::func_static(), "\n";
$a = new A;
$b = new B;
echo '$b->prop_instance: ', $b->prop_instance, "\n";
//not recommended (static method called as instance method):
echo '$b->func_static(): ', $b->func_static(), "\n";
echo '$b->func_instance():', "\n", $b->func_instance(), "\n";
/* This is more tricky
* in the first case, a static call is made because $this is an
* instance of A, so B::dyn() is a method of an incompatible class
*/
echo '$a->dyn():', "\n", $a->callDynamic(), "\n";
/* in this case, an instance call is made because $this is an
* instance of B (despite the fact we are in a method of A), so
* B::dyn() is a method of a compatible class (namely, it's the
* same class as the object's)
*/
echo '$b->dyn():', "\n", $b->callDynamic(), "\n";
输出:
B::$prop_static: B::$prop_static value
B::func_static(): in B::func_static
$b->prop_instance: B::$prop_instance value
$b->func_static(): in B::func_static
$b->func_instance():
in B::func_instance
in A::func_instance
in A::func_instance
$a->dyn():
in A::callDynamic
in dynamic dyn (__callStatic)
$b->dyn():
in A::callDynamic
in dynamic dyn (__call)
B::$prop_静态:B::$prop_静态值
B::func_static():在B::func_static中
$b->prop_实例:b::$prop_实例值
$b->func_static():在b::func_static中
$b->func_实例():
在B::func_实例中
在::func_实例中
在::func_实例中
$a->dyn():
在A::callDynamic中
在动态动态中(_callStatic)
$b->dyn():
在A::callDynamic中
在动态动态动态中(uu调用)
如果左侧部分是对象实例,则使用
->
。否则,使用:
这意味着
->
主要用于访问实例成员(尽管它也可以用于访问静态成员,但不鼓励这样的使用),而::
通常用于访问静态成员(尽管在一些特殊情况下,它用于访问实例成员)
通常,::
用于,它可能有一个类名,父类
,自身
,或者(在PHP5.3中)静态
parent
指使用它的类的超类的范围self
指使用它的类的范围<代码>静态指的是“被调用的范围”(请参阅)
规则是使用:
的调用是实例调用,当且仅当:
- 目标方法未声明为静态和静态
- 调用时存在兼容的对象上下文,这意味着这些上下文必须为true:
- 调用是在
存在且$this
的类是被调用方法的类或其子类$this
class A {
public function func_instance() {
echo "in ", __METHOD__, "\n";
}
public function callDynamic() {
echo "in ", __METHOD__, "\n";
B::dyn();
}
}
class B extends A {
public static $prop_static = 'B::$prop_static value';
public $prop_instance = 'B::$prop_instance value';
public function func_instance() {
echo "in ", __METHOD__, "\n";
/* this is one exception where :: is required to access an
* instance member.
* The super implementation of func_instance is being
* accessed here */
parent::func_instance();
A::func_instance(); //same as the statement above
}
public static function func_static() {
echo "in ", __METHOD__, "\n";
}
public function __call($name, $arguments) {
echo "in dynamic $name (__call)", "\n";
}
public static function __callStatic($name, $arguments) {
echo "in dynamic $name (__callStatic)", "\n";
}
}
echo 'B::$prop_static: ', B::$prop_static, "\n";
echo 'B::func_static(): ', B::func_static(), "\n";
$a = new A;
$b = new B;
echo '$b->prop_instance: ', $b->prop_instance, "\n";
//not recommended (static method called as instance method):
echo '$b->func_static(): ', $b->func_static(), "\n";
echo '$b->func_instance():', "\n", $b->func_instance(), "\n";
/* This is more tricky
* in the first case, a static call is made because $this is an
* instance of A, so B::dyn() is a method of an incompatible class
*/
echo '$a->dyn():', "\n", $a->callDynamic(), "\n";
/* in this case, an instance call is made because $this is an
* instance of B (despite the fact we are in a method of A), so
* B::dyn() is a method of a compatible class (namely, it's the
* same class as the object's)
*/
echo '$b->dyn():', "\n", $b->callDynamic(), "\n";
输出:
B::$prop_static: B::$prop_static value
B::func_static(): in B::func_static
$b->prop_instance: B::$prop_instance value
$b->func_static(): in B::func_static
$b->func_instance():
in B::func_instance
in A::func_instance
in A::func_instance
$a->dyn():
in A::callDynamic
in dynamic dyn (__callStatic)
$b->dyn():
in A::callDynamic
in dynamic dyn (__call)
B::$prop_静态:B::$prop_静态值
B::func_static():在B::func_static中
$b->prop_实例:b::$prop_实例值
$b->func_static():在b::func_static中
$b->func_实例():
在B::func_实例中
在::func_实例中
在::func_实例中
$a->dyn():
在A::callDynamic中
在动态动态中(_callStatic)
$b->dyn():
在A::callDynamic中
在动态动态动态中(uu调用)
=>
运算符用于在关联数组中分配键值对。例如:
$fruits = array(
'Apple' => 'Red',
'Banana' => 'Yellow'
);
在foreach
语句中,其含义类似:
foreach ($fruits as $fruit => $color)
echo "$fruit is $color in color.";
=>
运算符用于在关联数组中分配键值对。例如:
$fruits = array(
'Apple' => 'Red',
'Banana' => 'Yellow'
);
在foreach
语句中,其含义类似:
foreach ($fruits as $fruit => $color)
echo "$fruit is $color in color.";
:
用于静态上下文,即当某个方法或属性声明为静态时:
class Math {
public static function sin($angle) {
return ...;
}
}
$result = Math::sin(123);
另外,调用父类的方法/属性时,会在动态上下文中使用::
运算符(范围解析运算符,也称为a.k.a):
class Rectangle {
protected $x, $y;
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
}
class Square extends Rectangle {
public function __construct($x) {
parent::__construct($x, $x);
}
}
->
用于动态上下文,即当您处理某个类的某个实例时:
class Hello {
public function say() {
echo 'hello!';
}
}
$h = new Hello();
$h->say();
顺便说一句:当你没有OOP经验时,我不认为使用Symfony是个好主意
:
用于静态上下文,即当某个方法或属性声明为静态时:
class Math {
public static function sin($angle) {
return ...;
}
}
$result = Math::sin(123);
另外,调用父类的方法/属性时,会在动态上下文中使用::
运算符(范围解析运算符,也称为a.k.a):
class Rectangle {
protected $x, $y;
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
}
class Square extends Rectangle {
public function __construct($x) {
parent::__construct($x, $x);
}
}
->
用于动态上下文,即当您处理某个类的某个实例时:
class Hello {
public function say() {
echo 'hello!';
}
}
$h = new Hello();
$h->say();
顺便说一句:当你没有OOP经验时,我不认为使用Symfony是个好主意 对于那些刚开始在PHP5中使用OOP PHP的人来说,静态方法和属性与实例化方法和属性之间的差异似乎是最大的障碍之一 从静态上下文调用对象或属性时,使用双冒号运算符(希伯来语-trivia中称为Paamayim Nekudotayim)。这意味着尚未创建对象的实例 相反,arrow操作符调用从对象实例的引用获取的方法或属性 静态方法在链接到数据库以创建和删除方法的对象模型中特别有用,因为您可以将返回值设置为插入的表id,然后使用构造函数按行id实例化对象。