::(双冒号)和->有什么区别? (箭头)在PHP中? [英] What's the difference between :: (double colon) and -> (arrow) in PHP?

查看:113
本文介绍了::(双冒号)和->有什么区别? (箭头)在PHP中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中有两种不同的访问方法的方法,但是有什么区别?

There are two distinct ways to access methods in PHP, but what's the difference?

$response->setParameter('foo', 'bar');

sfConfig::set('foo', 'bar');

我假设->(带有大于号或V形的破折号)用于变量函数,而::(双冒号)用于类函数.正确吗?

I'm assuming -> (dash with greater than sign or chevron) is used for functions for variables, and :: (double colons) is used for functions for classes. Correct?

=>赋值运算符是否仅用于在数组内分配数据?与用于实例化或修改变量的=赋值运算符相反吗?

Is the => assignment operator only used to assign data within an array? Is this in contrast to the = assignment operator which is used to instantiate or modify a variable?

推荐答案

当左侧是对象实例时,请使用->.否则,请使用::.

When the left part is an object instance, you use ->. Otherwise, you use ::.

这意味着->主要用于访问实例成员(尽管它也可以用于访问静态成员,不建议使用这种用法),而::通常用于访问静态成员(尽管少数访问)特殊情况下,它用于访问实例成员.)

This means that -> is mostly used to access instance members (though it can also be used to access static members, such usage is discouraged), while :: is usually used to access static members (though in a few special cases, it's used to access instance members).

通常,::用于范围分辨率,它的左侧可能是类名称parentself或(在PHP 5.3中)static. parent是指使用它的类的超类的范围; self指的是使用该类的范围; static指的是被称为作用域"(请参阅​​ late静态绑定).

In general, :: is used for scope resolution, and it may have either a class name, parent, self, or (in PHP 5.3) static to its left. parent refers to the scope of the superclass of the class where it's used; self refers to the scope of the class where it's used; static refers to the "called scope" (see late static bindings).

规则是,只有在以下情况下,使用::进行的调用才是实例调用:

The rule is that a call with :: is an instance call if and only if:

  • 目标方法未声明为静态
  • 在调用时有一个兼容的对象上下文,这意味着它们必须为真:
  • the target method is not declared as static and
  • there is a compatible object context at the time of the call, meaning these must be true:
  1. 该调用是在$this存在且
  2. 存在的上下文中进行的
  3. $this的类是被调用方法的类或它的子类.
  1. the call is made from a context where $this exists and
  2. the class of $this is either the class of the method being called or a subclass of it.

示例:

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)

这篇关于::(双冒号)和->有什么区别? (箭头)在PHP中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆