0

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 e.g.

$myclass->method()

or

MyClass::method()

what is the difference?

Community
  • 1
  • 1
Cameron
  • 24,868
  • 91
  • 263
  • 449
  • 2
    take a look at http://stackoverflow.com/questions/6313783/what-is-the-notation-in-php-used-for – Pav Jun 11 '11 at 12:28
  • also see: [In PHP, whats the difference between :: and -> ?](http://stackoverflow.com/questions/3173501/in-php-whats-the-difference-between-and) – ldg Jun 11 '11 at 12:38

3 Answers3

2

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.

Vincent Ramdhanie
  • 98,815
  • 22
  • 134
  • 183
1

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

Community
  • 1
  • 1
Nanne
  • 61,952
  • 16
  • 112
  • 157
0

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.

PeeHaa
  • 66,697
  • 53
  • 182
  • 254