Quantcast
Channel: #! code - PHP Questions
Viewing all articles
Browse latest Browse all 10

PHP Question: Class Methods

$
0
0

Question

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

1
2
3
4
5
// 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 instansiated first. It would be written like this.

1
2
3
4
5
6
7
8
9
10
11
<?php// Define classclass MyClass {    publicfunction MyMethod(){    }} 
// Instansiate 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 instansiate the object first. It would be written like this (notice the static keyword in the method declaration):

1
2
3
4
5
6
7
8
9
<?php// Define classclass 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 instansiated 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
2
3
4
// Define classclass MyClass {    public static $var=3;}

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

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

Category: 

Viewing all articles
Browse latest Browse all 10

Trending Articles