Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class question - Beginner help [duplicate]

Tags:

php

class

Possible Duplicate:
In PHP, whats the difference between :: and -> ?

This a continuation from my previous question - however I think its unique enough to warrant a new question.

What is the difference between:

Message::listMessages(); 

and

$message->listMessages(); 

I'm creating a mini-cms and I want a system that displays errors in a uniform fashion.

Cheers, Keiran

like image 236
Keiran Lovett Avatar asked Mar 26 '26 07:03

Keiran Lovett


2 Answers

Static methods come handy when we want to share information between objects of a class, or want to represent something that's related to the class itself, not any particular object.

The difference between the two is in the way they are invoked. For example, Message::listmessages() is a static method and can be called like this:

$messages = Message::listmessages($args);

You do not need to first make an object of class Message, in order to use the above. Also, note that this should be used when you want to return a result on definite pre-configured variables, and is not based on properties of class Message

However, $message->listmessages() is an instance method and can be called like this:

$message = new Message();
$messages->$args = $args
$messages= $message->listmessages();

This is used for generic occassions when you want to call a function on runtime properties of class Message.

like image 155
Stoic Avatar answered Mar 27 '26 21:03

Stoic


As i understood your question,

we are using this way Message::listMessages(); in C and C++

but right syntax we are using in PHP is $message->listMessages();

Thanks.

like image 41
Chandresh M Avatar answered Mar 27 '26 21:03

Chandresh M