PHP Question: Class Methods

4th May 2011

Question

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

  1. // Line 1
  2. $MyClass->MyMethod();
  3.  
  4. // Line 2
  5. 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.

  1. <?php
  2. // Define class
  3. class MyClass {
  4. public function MyMethod() {
  5. }
  6. }
  7.  
  8. // Instantiate object
  9. $MyClass = new MyClass();
  10. // Call method
  11. $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):

  1. <?php
  2. // Define class
  3. class MyClass {
  4. public static function MyMethod() {
  5. }
  6. }
  7.  
  8. // Call static method
  9. 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.

  1. <?php
  2. // Define class
  3. class MyClass {
  4. public static $var = 3;
  5. }

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

  1. // Create object.
  2. $myobject = new MyClass();
  3. // Access static property.
  4. 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:

  1. // Define class
  2. class MyClass {
  3. public static $var = 3;
  4.  
  5. public function MyMethod() {
  6. $this->var = 5;
  7. }
  8. }
  9.  
  10. // Create object
  11. $myobject = new MyClass();
  12. // Access static property
  13. 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

Permalink

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

Submitted by Jan on Thu, 09/08/2011 - 18:32

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.