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

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


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

我假设->(带大于号或人字形的破折号)用于变量函数,而::(双冒号)用于类函数。正确?
=>赋值运算符是否仅用于在数组内分配数据?这与用于实例化或修改变量的=赋值运算符相反吗?

最佳答案

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

这意味着->主要用于访问实例成员(尽管它也可以用于访问静态成员,但不鼓励此类用法),而::通常用于访问静态成员(尽管在一些特殊情况下,它用于访问静态成员)。实例成员)。

通常,::用于scope resolution,它的左侧可能有一个类名parentselfstatic(在PHP 5.3中)。 parent指的是使用该类的父类(super class)的范围; self指的是使用该类的范围; static指“被称为作用域”(请参阅​​late static bindings)。

规则是,仅在以下情况下,使用::的调用才是实例调用:

  • 目标方法未声明为static和
  • 在调用时有一个兼容的对象上下文,这意味着这些必须为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值
    B::func_static():在B::func_static中

    $ b-> prop_instance:B::$ prop_instance的值
    $ b-> func_static():在B::func_static中

    $ b-> func_instance():
    在B::func_instance
    在A::func_instance
    在A::func_instance

    $ a-> dyn():
    在A::callDynamic中
    在动态dyn(__callStatic)中

    $ b-> dyn():
    在A::callDynamic中
    在动态dyn(__call)中

    08-06 23:02