|Ten minutes To PHP Objects|
|Written by Mike James|
|Thursday, 18 November 2021|
Page 2 of 4
Things really start to be interesting when you add methods to classes.
Roughly speaking a property is something that a class has as a value and a method is something a class can do. You need to think of this difference as being like nouns and verbs. For example, a ball class might have a colour property and a bounce method, i.e. some code that makes it bounce. In other words, methods are what your class does.
In PHP methods are implemented using standard functions.
For example, our calculator class can quite easily have an add method added to it:
The function is declared using the Public keyword so that it can be used from outside fo the object.
Now we can create an instance of the class and use its add method:
$mycalc = new Calculator;
Notice that methods are accessed using the same “->” notation. As in the case of properties such as $answer, each instance of the class gets its own private copy of add, or any other method for that matter.(Again this isn't 100% true but a good way to think about things until you move on to more sophisticated ways of working with objects and classes.)
You really do have to think of a class as a specification for creating an object.
Self reference - this
That’s more or less all there is to basic objects in PHP, but there are some subtle points that we need to cover to make sure that you know how everything works.
The first little problem to overcome is how does a class refer to its own methods and properties. At first you might not see the need for a class to refer to its own methods and properties - after all its seems a little strange.
In fact it is perfectly normal for a class to want to reference its own properties and methods but it isn't quite a straightforward as you might expect.
You might think that the following would work just fine:
But $answer in the function add isn’t the same as the $answer property defined outside of the function.
You can check this by adding the following lines:
You will see 3 and 10 printed out, demonstrating that the add method doesn’t change the answer property.
If you want a class method to use a property or another method then you have to make use of the “$this” keyword as in:
The use of $this can be thought of as specifying the current instance of the object created by the class. So if the current object is $mycalc the instruction $this->answer is converted to $mycalc->answer.
This is not only a good way to think about it as it actually corresponds to what happens. When you create an instance of a class $this gets set to point to the instances properties and methods.
The correct way to set the answer property in the Calculator class is:
Notice that you need to use $this everywhere you want to refer to the property of the current instance i.e. you need to change the return statement as well.
Another detail of using classes and objects is the constructor.
A constructor is simply a special method __construct which is called when an instance of the class is created. Notice that the word construct is prefixed with two underline characters.
The constructor is essentially an initialisation routine. For example:
public function __construct()
is a constructor for the Calculator class that simply sets the answer property to zero.
Constructors can have parameters.
public function __construct($a)
This constructor sets the answer property to whatever $a is at the time an instance is created. You can have as many parameters as you care to define.
This raises the question of how you specify parameters when you create a class with such a constructor. The answer is that you simply include the parameters in the line that creates the object. For example:
creates an instance of the calculator class with answer set to 4.
If you are familiar with other languages you might be wondering if it is possible to create multiple constructors in PHP each with a different signature? The answer is that constructor overloading isn't supported but you can implement the same sort of thing by manually examining the arguments passed to a constructor.
|Last Updated ( Thursday, 18 November 2021 )|