what's the difference?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
global_erp_solution
Forum Commoner
Posts: 25
Joined: Sun Jul 08, 2012 6:47 am

what's the difference?

Post by global_erp_solution »

What's the different effect when accessing member using

Code: Select all

$this::doIt();
and

Code: Select all

$this->doIt();
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: what's the difference?

Post by social_experiment »

First time i have seen this approach but i would say there's no difference
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: what's the difference?

Post by requinix »

First one is a static call on whatever type of object $this is (as opposed to a static call using the actual name of the class). Since it's $this you should use self instead.

Code: Select all

self::doIt();
Second is an instance method call.
social_experiment wrote:First time i have seen this approach but i would say there's no difference
:banghead:
global_erp_solution
Forum Commoner
Posts: 25
Joined: Sun Jul 08, 2012 6:47 am

Re: what's the difference?

Post by global_erp_solution »

thanks for the reply requinix, does this mean I use :: only when referring to any static functions or members only? what's the side effect if I use :: to call instance member and functions?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: what's the difference?

Post by social_experiment »

:lol:
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: what's the difference?

Post by requinix »

global_erp_solution wrote:does this mean I use :: only when referring to any static functions or members only?
Yes.
global_erp_solution wrote:what's the side effect if I use :: to call instance member and functions?
Method: It's called statically (no $this) and you get a Strict "Non-static method Class::method() should not be called statically".
Variable: PHP will look for a static member variable, not find it, and crash with a Fatal "Access to undeclared static property Class::$variable".
Post Reply