Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Declaring Static Methods in PHP

DZone's Guide to

Declaring Static Methods in PHP

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

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!

 

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}