Close

January 2, 2018

Static Methods and Properties In PHP

In my last tutorial, What Object-Oriented Programming Is NOT, I mentioned that over-using or mis-using the static keyword is one way developers get confused with object-oriented programming. That, often, they simply take their procedural code, dump into a class, make everything static and away we go.

That said, there CAN be legitimate uses for fully static classes.

I like the answer Pascal MARTIN gave on StackOverflow:

The distinction I make is this:

  • Use an instantiated class for actual objects in your applications (post, category, user, etc)
  • Use a static class for “libraries of code” that have useful functions

As Pascal said, that’s probably over-simplified, but it helps me keep my head straight. Of course, the question becomes… how do we write a static class.

The first thing to keep in mind is “once you go static, you can’t go back”… in a way. More specifically, you can’t use object properties or methods in static methods. So, this won’t work:

<?php
class Library {
    public $var = 'Hey';
    
    public static function do_stuff() {
        echo $this->var;
    }
}

Library::do_stuff(); // Triggers: Fatal error: Using $this when not in object context

That’s because $this references and instantiated object. But, we bypass that when using static methods. So, $this essentially doesn’t “exist”. But, you might think you could do this:

<?php
class Library {
    public $var = 'Hey';
    
    public static function do_stuff() {
        echo Library::$var;
    }
}

Library::do_stuff(); // Triggers: Fatal error: Access to undeclared static property: Library::$var

So, once you declare a method static… basically everything it touches has to be static, too. Thus, this is what works:

<?php
class Library {
    public static $var = 'Hey';
    
    public static function do_stuff() {
        echo Library::$var;
    }
}

Library::do_stuff(); // Outputs "Hey"

One way you might use this, then, is something like this:

<?php
class Library {
    public static $date_format = 'F jS, Y';
    
    public static function format_date($unix_timestamp) {
        return date(self::$date_format, $unix_timestamp);
    }
}

echo Library::format_date(time()); // Outputs formatted date

The format_date() method is one that could be used in various places throughout your application and wouldn’t need constantly re-instantiated to use. And, of course, this Library class could contain various, similar methods that have data-agnostic uses. A kind of “catch-all” for miscellaneous functions.

And, of course, as you’ve seen the way to make a property or method static is to simply add the “static” keyword to the declaration, like so:

<?php
class Library {
    public static $var = 'Hey';
    
    public static function do_stuff() {
        // do stuff
    }
}

So, there you go. Now, if you want to keep going and learn how to build professional PHP applications using object-oriented programming, then check out my full object-oriented programming course here (also available on Udemy here).

One Comment on “Static Methods and Properties In PHP

Douglas Heller
January 3, 2018 at 9:26 pm

Hey John.
Enjoyed this. In some of the other programming languages I’ve studied I’ve used static properties/methods in classes which would be routinely accessed in various parts of the application. An example would be CDatabaseUtilities in which the database connection object and all other database functions would be defined. Another would be a CUtilities class in which various form validation methods and other general, commonly used utilities can be defined. In doing so you cut down on redundant coding as well as provide more modularization. Many of these methods are easily transported to other applications as well. I’ll be looking forward to doing something similar once I get to work on your OOP course which I’m hoping will be within the next week. Many thanks.

PS: I thought you were pretty funny. Made me laugh.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *