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?

2

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.