|Ten minutes to PHP objects|
|Written by Ian Elliot|
|Monday, 30 May 2011|
Page 1 of 3
PHP is a fully object-oriented language with lots of powerful features. This introductory guide looks at how PHP handles objects.
Many PHP programmers simply ignore its most powerful feature – it is fully object oriented. When PHP was first introduced it was a simple scripting language but over time it has developed into a fully fledged language with all the usual modern conveniences.
However many PHP programmers still treat it as a sort of improved scripting language.
Let’s take a look at how PHP handles objects.
In this ten minute guide - which will probably in all honesty take you longer than ten minutes to read - it is assumed that you already have a PHP environment setup and working. If you want help with getting started see Ten Minutes to PHP.
Class and objects
If you have been using scripting in web pages you might think that object-oriented programming is overkill for what you mostly want to do.
You can, of course, opt to use PHP without ever worrying about objects, but they aren’t difficult to work with and they provide a way of organising your code so that it is easier to reuse and modify. Even if you do try to avoid them you will probably encounter them sooner or later as part of some library or framework that you decide to use - so better to come to terms with them now.
The following tutorial should give you a clear idea of what objects have to offer.
Before we get started it is important to distinguish between a class and an object.
A class is a specification for an object and once you have created a class you can use it to create as many objects, of that type, as you need.You can think of a class as a "cookie cutter" that you can use to stamp out as many cookies as you need in that particular shape.
In this sense a class doesn’t actually do anything. It isn’t really executable PHP code (although this isn't quite true in a more advanced use of class code).. It basically just sits there waiting for someone to make use of it.
For example, let’s suppose we need a calculator that can be used in any web page. So we might well proceed by defining a calculator class.
The class keyword is used to create a new class and the definition of the class goes between the curly brackets.
In general an object can have properties and methods defined within the class. You can think of properties as being values or variables that relate to the object.
For a calculator a property must be a variable like $answer which could be used to record the last answer. Properties behave like variables that belong to the class or object. They have to be declared using the public keyword if you want them to be accessible outside of the object.
For example to give the calculator class an “answer” property, initialised to zero, you would write:
Now we have a class with a property we can try it out.
To create an object from a class we use new the command:
Whenever you have a class you can use the same sort of instruction to create an instance of the class. Each instance you create has all of the properties and functions that you define in the class. You can create as many instances of a class you require. Once you have created an instance you can access its properties using expressions like:
This can be read “mycalc’s answer property”. Notice that it is the name of the instance or the object that you use and not the class.
Properties can be used just like variables. For example:
Every instance of a class you create has its own properties and methods. For example, if you try:
then you will see 10 and 20 printed. This demonstrates that each instance is a complete and distinct incarnation of the class that you have defined.
At this point you should be happy with the idea that the class defines the object in the sense that it determines what properties and methods an object created using it will have. To create an instance of an object you simply use
and every time you create an instance the object has a completely new set of properties and methods that are independent of any other instance. The properties of an object behave just like variables but you have to refer to them using a "fully qualified" name:
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.
|Last Updated ( Thursday, 18 February 2016 )|