PHP Question: Class Methods

Question

What is the difference between these two lines of code and can you produce the background code used for them?

// Line 1
$MyClass->MyMethod();

// Line 2
MyClass::MyMethod();









 

Answer

The first line of code is a normal object method call and requires that the object be instantiated first. It would be written like this.

<?php
// Define class
class MyClass {
    public function MyMethod() {
    }
}

// Instantiate object
$MyClass = new MyClass();
// Call method
$MyClass->MyMethod();

The second line of code is a static method call, which can be used without having to instantiate the object first. It would be written like this (notice the static keyword in the method declaration):

<?php
// Define class
class MyClass {
    public static function MyMethod() {
    }
}

// Call static method
MyClass::MyMethod();

The -> symbol is used to access the methods and properties of an object, whereas the :: symbol is used to access only static methods and properties.

Defining a method as static doesn't require that you use it statically, but doing so should really be avoided. You can have an object with a mix of static and non-static methods but you have to be careful with what you are doing or you will find errors appearing in your code. For example, if you have a static method in a class and you try to access the object itself using $this (even if you have first instantiated the object) you will get the following fatal error.

Fatal error: Using $this when not in object context in test.php on line x

This is the most important consideration when using static methods. They will not be called in an object context, which means that you can't use the $this keyword to reference the object itself so you can't access any other method or property in the class unless they are also static.

You can also find errors appearing if you try to access static properties in a non-static context. For example, take the following class definition containing a single static variable.

<?php
// Define class
class MyClass {
    public static $var = 3;
}

If you try and instantiate the object and then access the static property to print it in a non-static context:

// Create object.
$myobject = new MyClass();
// Access static property.
echo $myobject->var;

You will get the following notice and nothing will be printed out (i.e. the property will not be accessed).

Notice: Undefined property: MyClass::$var in test.php on line x

If you try to access the property from within the class in a non-static context by using $this->var from within a method:

// Define class
class MyClass {
    public static $var = 3;
    
    public function MyMethod() {
      $this->var = 5;
    }
}

// Create object
$myobject = new MyClass();
// Access static property
echo $myobject->MyMethod();

Everything will work as expected but PHP will also issue you with a strict standards warning.

Strict standards: Accessing static property MyClass::$var as non static in test.php on line x

It is therefore generally good practise to have static methods and properties defined in their own class so that you don't start mixing them up and trying to access static properties in a non-static context. If you want to change the value of a static variable at run time then maybe it shouldn't be static after all.

Comments

I´m missing an example how to access a static property using "self::" as this would solve the strict warning in the last example.

Greats from Germany
Jan

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
8 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.