Page 1 of 1

what's the difference?

Posted: Fri Jul 27, 2012 1:28 am
by global_erp_solution
What's the different effect when accessing member using

Code: Select all

$this::doIt();
and

Code: Select all

$this->doIt();

Re: what's the difference?

Posted: Fri Jul 27, 2012 1:40 am
by social_experiment
First time i have seen this approach but i would say there's no difference

Re: what's the difference?

Posted: Fri Jul 27, 2012 3:27 am
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:

Re: what's the difference?

Posted: Fri Jul 27, 2012 3:33 am
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?

Re: what's the difference?

Posted: Fri Jul 27, 2012 4:05 am
by social_experiment
:lol:

Re: what's the difference?

Posted: Fri Jul 27, 2012 1:29 pm
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".