PHP Using classes
Possible Duplicates:
Reference - What does this symbol mean in PHP?
PHP: Static and non Static functions and Objects
In PHP, whats the difference between :: and -> ?
I have seen different ways to use classes in PHP eg
$myclass->method()
or
MyClass::method()
what is the difference?
From your example, $myclass appears to be an instance of the class MyClass and you are invoking an instance method. Instance methods are invoked from instances of a class.
In the second example, method appears to be a static method of the class. A static method is invoked at the class level, no instance is necessary.
The first is calling method from an object, so you would have done $myclass = new MyClass()
, the constructor ( __construct()
) was called, etc.
The second one is a static call: no object is instantiated, and it cannot use $this
references. Static variables are the same all over the place btw, while non-static variables are specific to the object they're in.
Although the question is closed, you might find some good info on static here: https://stackoverflow.com/questions/3090994/what-does-the-static-keyword-mean-in-oop
To be able to use $myclass->method()
you first have to create an instance of the class.
$myclass = new myClass();
The second is used to access the moethod without first creating an instance.
链接地址: http://www.djcxy.com/p/58020.html上一篇: 访问类方法的PHP区别
下一篇: PHP使用类