::(双冒号)和。之间的区别是什么?

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

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

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

我假设-> (破折号大于符号或V形)用于变量的函数,并且:: :(双冒号)用于类的函数。 正确?

=>赋值运算符仅用于在数组内分配数据吗? 这是在对比的是=被用于实例化或修改变量赋值运算符?


当左边部分是对象实例时,可以使用-> 。 否则,您使用::

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

通常, ::用于作用域解析,它可以在其左边是static的类名, parent类, self或(在PHP 5.3中)。 parent指的是它所使用的类的超类范围; self指使用它的类的范围; static是指“调用范围”(请参阅​​后期静态绑定)。

规则是::的调用是实例调用当且仅当:

  • 目标方法没有声明为静态的
  • 在调用时有一个兼容的对象上下文,这意味着它们必须是真的:
  • 该调用是从$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)
    

    ::静态上下文中使用,即。 当某些方法或属性被声明为静态时:

    class Math {
        public static function sin($angle) {
            return ...;
        }
    }
    
    $result = Math::sin(123);
    

    另外,当您调用父类的方法/属性时, ::运算符(范围解析运算符,又名Paamayim Nekudotayim)用于动态上下文中:

    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 Test {
    
        public $name;
    
        public function __construct() {
            $this->name = 'Mrinmoy Ghoshal';
        }
    
        public static function doWrite($name) {
            print 'Hello '.$name;
        }
    
        public function write() {
            print $this->name;
        }
    }
    

    这里doWrite()函数不依赖于任何其他方法或变量,而且它是一个静态方法。 这就是为什么我们可以通过这个操作符调用这个方法,而不需要初始化这个类的对象。

    Test::doWrite('Mrinmoy'); // Output: Hello Mrinmoy.

    但是,如果您想以这种方式调用write方法,则它将生成一个错误,因为它依赖于初始化。

    链接地址: http://www.djcxy.com/p/1749.html

    上一篇: What's the difference between :: (double colon) and

    下一篇: What's the meaning of the PHP token name T