Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Lorna Jane Mitchell is a PHP developer, blogger, trainer and evangelist from Leeds in the UK. She is active with phpwomen.org and her local user group PHP North West, and writes for a variety of outlets, including her own blog at lornajane.net. She is an active member of the PHP and open source communities and contributes to the joind.in event feedback project. When she's not at her computer, Lorna enjoys yarn craft, hobby electronics, and her home renovation project. Lorna is a DZone MVB and is not an employee of DZone and has posted 33 posts at DZone. You can read more from them at their website. View Full User Profile

Declaring Static Methods in PHP

08.27.2011
Email
Views: 2745
  • submit to reddit

I was confused recently to realise that I had accidentally called a static method in PHP dynamically from another part of my code; I expected PHP to output warnings when this is done. On closer inspection I discovered that:

  • Static functions can be called dynamically
  • Dynamic functions generate an E_STRICT error if called statically

This made a lot more sense when I thought about it a bit more and wrote some toy code:

class MyClass
{
    static function mystatic() {
        echo "this was declared static!\n";
        return true;
    }                                                                                                                                
    function mydynamic() {
        echo "a dynamic function said this\n";
        return true;
    }
}                                                                                                                                    
$obj = new MyClass();                                                                                                                
$obj->mydynamic();
MyClass::mydynamic();                                                                                                                
MyClass::mystatic();
$obj->mystatic();

When run with error_reporting set to E_ALL | E_STRICT, this outputs the following:

a dynamic function said this

Strict Standards: Non-static method MyClass::mydynamic() should not be called statically in /home/lorna/data/personal/publish/blog_stuff/static-functions.php on line 22
a dynamic function said this
this was declared static!
this was declared static!

 

Calling a Dynamic Method Statically

This generates the E_STRICT error, because a dynamic method expects to be in an object context, and with a static call this isn't the case. Methods to be called statically ought to be declared as such - it means we won't have access to $this, and using it in a static method generates an error to warn us that something is wrong. I think it makes much more sense to state whether the method is intended to be used statically or not - as much as an indicator to future programmers as anything else ("future programmers" includes me if I sleep between declaring the function and using it!)

Calling a Static Method Dynamically

This does not generate an error and for a while I thought this was a mistake, but actually it does make sense. A static method does not rely on the object context, it just takes inputs and generates outputs - and if it refers to static properties or other static methods then it does so in a way that is independent of how the method is called. So although there is probably something wrong if a static method is being called dynamically, it's unlikely to cause problems or be dangerous in the same way that calling a method intended for dynamic use in a static context could be.

I tripped myself up with this so I thought I would put it on my blog to remind me in the future which way round it is!

 

References
Tags:
Published at DZone with permission of Lorna Mitchell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Nabeel Manara replied on Fri, 2012/01/27 - 12:41pm

Wow great i have read many articles about this topic and everytime i learn something new i dont think it will ever stop always new info, good job.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.