views:

50

answers:

4

In my company's codebase, i see functions used in both static and object context. For e.g. a class A has a function b() which is called both using A::b() and/or object_of_type_A->b(). I know this throws an error if strict is turned on. But I wanted to know if this is a bad practice and if yes, then why? Thanks for any answers.

Let me know if I don't make sense anywhere. I would be happy to clarify.

+5  A: 

I'm not a php guy, but this sounds just like Java, where it's allowed but discouraged.

If it's static, I would strongly recommend only calling it in a static way. Otherwise it looks like it depends on the state of the object you're supposedly calling it on.

In Java the best example of this is Thread.sleep(). It's a static method which puts the current thread to sleep, always. But look at this code:

Thread t = new Thread(someTask);
t.start();
t.sleep(1000);

What does it look like that code is doing? It appears to be putting the other thread to sleep, whereas in fact it'll be the current thread that's sleeping. When you change it to a plain static call, it's more obvious:

Thread.sleep(1000);

That doesn't refer to t, so must be about the current thread.

Unless there's something specific to php where calling the static method via a variable gives you some sort of polymorphism, I suggest you stick to calling it in the static way. The fact that strict mode tells you to do this is a pretty strong hint, IMO :)

Jon Skeet
there is no polymorphism involved. the function is called in both static as well object contexts. i am still not clear though if it is just best practice or any other issue is involved here. thanks for the answer though.
pinaki
+1  A: 

There is 'currently' no harm in using it either way except of course when called as a static function you can't access the $this member.

The reason it errors in strict is because not writing your code to strict standards can result in errors occurring due to a lack of diligence. in the future it may also cause your code to break. a static function has no $this member and it may break parameter passing.

Play it safe only call static functions with A::b() type calls.

DC

DeveloperChris
thanks for the answer. currently, it does not use $this inside the strict function.
pinaki
A static member function cannot expect $this to be valid and therefore should never use the $this pointer. by way of comparison c++ has static members and non static members although it normally uses an implicit this pointer you can call the static member function using this->staticfunction() with no problems. but if you tried to use 'this' inside the static member function it would result in a compile time error because this is not valid in a static function
DeveloperChris
+1  A: 

Here's some test code:

<?php

error_reporting(E_ALL | E_STRICT);

class Foo{
    public function a(){
    }
    public static function b(){
    }
}

$MyFoo = new Foo;
Foo::a(); // Strict Standards: Non-static method Foo::a() should not be called statically
Foo::b();
$MyFoo->a();
$MyFoo->b(); // No complaints

?>

PHP/5.3 warns about static calls to non-static methods, which is fine since they are subject to failure as soon as you want to access $this. But it does not complain about object context calls to static functions: there's nothing that can go wrong. This behaviour is documented:

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can) [...] Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.

So, as far as PHP is concerned, what you found in the code base is not wrong. However, I think it's slightly confusing.

Álvaro G. Vicario
+1, i think this is exactly what i was looking for... so, the behavior is correct but confusing :-)... is there any other reason to be concerned??? Thanks for the answer..
pinaki
+1  A: 

Regarding accessing $this in a static function I found something a bit strange a while back (might be changed in later versions of PHP though, think I ran 5.2 or something).

You can read about it here but it's in swedish. But use google translate and it should be understandable.

http://www.phpportalen.net/viewtopic.php?p=560080#560080

inquam
+1, very interesting post (however unrelated to my problem ;-) )..
pinaki
I don't get the problem, even though he calls foo::getname() it is still called as a non static member and therefore $this points to a class type of 'foo' in the first call and 'bar' in the second, therefore the result he gets and calls weird is exactly what he should get.
DeveloperChris