What are PHP Static Functions (Methods) & Properties?

What are PHP Static Functions (Methods) & Properties?

This is largley thanks to Matt Zandstra's book " PHP Objects Patterns and Practice" buy his book: Amazon UK | Amazon US

As I feel more safe delving into frameworks it worries me when it leads to the failing to understand what's actually going on. There is no magic. It concerns me when I hear things like:

"It works, but I don't know why."

The word "magic" used in anything to do with programming including:

"It's just magic".

When you ask, how does it work?

"Oh, it's magic from framework xyx"

It's not magic. It's super cool.

When languages, tools or aproaches are compared and rated seemingly only on how well they are understood to a familier (and safe feeling) tool. Phrases include:

Oh x is <insert expletive here>.

With no reasoning.

Or the slightly more vailed:

Oh x is <insert expletive here> at doing y

When really its just because time hasn't been invested in understanding x to understand and use it well.

Static: Access methods & properties without instantiation

Without having to instantiate an object (new myObjectName()) you may access a class's methods and properties directly, without having created an instance of the object.

You can access both methods and properties in the context of a class rather than that of an object.
Matt Zandstra's

Note therefore, that because we're talking about class context, you can't access any object properties from a class context (e.g. if you had public, protected, private proprties, these only become avalable to you one an object has been instantiated (new myObjectName()). If you were to declare your classes proprties as static, then you would also be able to access them statically.

<?php
class Person
{
    public $firstName = 'Default First Name';
    static public $lastName = 'Default Last Name';
    public static function getFullName()
    {   
        print self::$lastName;
    }   
}

// This will work because both $lastName 
// and getFullName are declared as static
Person::getFullName();

// This will fail because $firstName is 
// an object property and we're tring to
// call it from a class context. This is what
// causes "Access to undeclared static property"
print Person::$firstName;

// To allow $firstName to be called statically, 
// the property must be declared as static e.g.
// "static public $firstName" within the class
// definition.

Why are static methods useful?

PHP static method examples.

  • Because you don't need to instantiate an object first, you may call static methods and properties right away. This is useful for object factories, for example.
  • When you do create an instance (an object) every instance of this class also has access to the class's static methods and properties so the functionalitiy is available to all instances.

PHP Objects Patterns and Practice gives an easily understood example of creating a static getInstance method which takes a database ID & PDO instance as arguments, and returns an object from the database. This is an example of the factory pattern, but the point here is that the static method getInstance has no dependiencies on an instantiated object, so is suited perfectly as an example of where static methods are useful.

Finally, some food for thought in video form, the opening line to this brilliant talk is:

I'm a firm believer that every conference should have one weird talk...

Enjoy.

This is largley thanks to Matt Zandstra's book " PHP Objects Patterns and Practice" buy his book: Amazon UK | Amazon US

Collect recurring payments with Subscribie - Try Now