What is the difference between :: and
Possible Duplicates:
Reference - What does this symbol mean in PHP?
In PHP, whats the difference between :: and -> ?
When you try to access property of method inside class whats the difference between :: and -> and is there full reference of operators related to object oriented programming in php somewhere?
The ::
is for static properties and methods, eg
MyClass::create();
The ->
is for when you have an object instantiated from the class, eg
$myObject = new MyClass;
$myObject->create();
When using ::
you can access a Class method statically without creating an instance of the class, something like:
Class::staticMethod();
You would use ->
on an instance of a Class, something like:
$class = new Class();
$class->classMethod();
It's the difference between static and dynamic properties and methods.
Observe this piece of code to appreciate the difference:
class MyClass {
protected $myvar = 0;
public static $othervar = "test";
public function start($value)
{
// because this is not static, we need an instance.
// because we have an instance, we may access $this
$this->myvar = $value;
// although we may still access our static variable:
echo self::$othervar;
}
static public function myOtherFunction ($myvar)
{
// its a static function, so we're not able to access dynamic properties and methods ($this is undefined)
// but we may access static properties
self::$overvar = $myvar;
}
}
Literature for your conveinience:
上一篇: > myMethod的(); vs MyClass :: myMethod();
下一篇: ::和。之间的区别是什么?