What's the difference between :: (double colon) and
There are two distinct ways to access methods in PHP, but what's the difference?
$response->setParameter('foo', 'bar');
and
sfConfig::set('foo', 'bar');
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).
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:
$this
exists and $this
is either the class of the method being called or a subclass of it. Example:
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";
Output:
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)
::
is used in static context, ie. when some method or property is declared as static:
class Math {
public static function sin($angle) {
return ...;
}
}
$result = Math::sin(123);
Also, the ::
operator (the Scope Resolution Operator, aka Paamayim Nekudotayim) is used in dynamic context when you invoke a method/property of a parent class:
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);
}
}
->
is used in dynamic context, ie. when you deal with some instance of some class:
class Hello {
public function say() {
echo 'hello!';
}
}
$h = new Hello();
$h->say();
By the way: I don't think that using Symfony is a good idea when you don't have any OOP experience.
Actually by this symbol we can call a class method that is static and not be dependent on other initialization...
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;
}
}
Here the doWrite() function is not dependent on any other method or variable, and it is a static method. That' why we can call this method by this operator without initializing the object of this class.
Test::doWrite('Mrinmoy'); // Output: Hello Mrinmoy.
But if you want to call the write
method in this way, it will generate an error because it is dependent on initialization.
上一篇: PHP中迟到的静态绑定究竟是什么?
下一篇: ::(双冒号)和。之间的区别是什么?