fcOnTheWeb Logo Web technologies, made easy.

fcOnTheWeb Red DotObject Oriented Programming in PHP

This article is going to serve as a very brief and basic introduction to object-oriented programming in PHP.

Firstly, there are a couple key concepts that need to be understood: the class and the object. Very simply put, the class is like the blueprint of a building. The blueprint is the plan, outlining what characteristics the building has, what makes up the building, etc. The object is the actual building. That is, it is the thing that is made from the plan. The building is the actual instance of the plan. The object is the instance of the class. And in the same way that one blueprint can create the same building in several different places, a single class can create many objects.

'Class' used in this context is not the same as an HTML or CSS class used on the client end of the model. If that does not make complete sense to you, do not worry as it is not important to this tutorial but it is helpful to know to avoid confusion.

So, now that we have a (very) basic understanding of classes and objects we will take a look at a class.

There are only two words that are needed to define a class in PHP. The key word class to declare it as such, and the name of the class, such as Person in the example below. By convention, classes generally start with a capital letter. While this is not the necessary syntax, it is the accepted way of doing things.

We can see an example of our Person class declaration below. We would typically put our class declarations in a separate file to our other code. You can have each class in a separate file, or just have one file which contains all your class declarations. It is up to you and depends on how you want to manage your code. We will be having each class declaration in a separate file - our Person class might be in personClass.php for example:

<php

class Person {

 

}

?>

Now that we have our basic class declared, we need to give it some properties. Our class properties are variables. These are similar to normal variables, but when they belong to an object we call them properties.

We need to declare in the class each of the properties it will have. For our person, these are some of the characteristics the person has. As each person will be an object, these are the properties the object has. We have chosen four for this example - name, age, gender and job.

We declare these properties inside the class like so:

private $name;

private $age;

private $gender;

private $job;

We use the key word private before each property to control the scope. We won't go into scope in this article, but basically it sets where variables or properties can be accessed from. By setting these properties as private it means they are only accessible from inside this class.

So, now that we have our properties set up, and we have set them to be only accessible from inside the class, how do we set or retrieve each property? We use functions which are called methods. Methods are just functions of the object. In the same way as an object's variables are called properties, its functions are called methods.

We need a method to both retrieve (or get) and set each property. These methods are referred to as 'getters' and 'setters'. They're the methods that get and set properties. They will start with the word get or set so they are recognisable.

The 'get' method (of our example) will simply return the value of the property. The 'set' method (of our example) will set the property to a new value which we will pass in.

Let's take a look at the 'getters' and 'setters' for each of the properties of our Person class.

public function set_name($new_name) {

$this->name = $new_name;

}

public function get_name() {

return $this->name;

}

public function set_age($new_age) {

$this->age = $new_age;

}

public function get_age() {

return $this->age;

}

public function set_gender($new_gender) {

$this->gender = $new_gender;

}

public function get_gender() {

return $this->gender;

}

public function set_job($new_job) {

$this->job = $new_job;

}

public function get_job() {

return $this->job;

}

You can see we have a unique method for each property. A 'get' and a 'set'. Our 'get' simply returns the value of the property, which we access through this. In PHP to access the properties or methods of objects we use an arrow like symbol which is a hyphen and a greater-than sign. -> This is different to other languages which traditionally use a dot notation to access properties.

Our 'get' and 'set' methods are fairly straightforward - the 'get' methods return the value of the property and the 'set' methods set the property to the new value which we pass in when we call it. We will cover how to call these methods shortly, once we have created an object.

We have started these methods with the keyword public so they are accessible outside the class by anything that calls them. Again, this is scoping issue we won't go into detail about here.

The last part of our class we need to add is the constructor. The constructor is the method (or function) that is automatically called when the object is created. In PHP it is defined with the name __construct. That is, two underscores, then "construct". This method will generally accept some values (but it doesn't have to) and will set values for some or all of the properties.

In our example, we will pass in a value for each of our four properties and set the properties to these values. That way, when a new person object is created, it will have all four properties populated.

This is the constructor we will use:

function __construct($new_name, $new_age, $new_gender, $new_job) {

$this->name = $new_name;

$this->age = $new_age;

$this->gender = $new_gender;

$this->job = $new_job;

}

You can see we pass in four values, one for each property, and then simply assign these values to their respective properties. This way, when a Person object is created all of their properties are set up from the start. We can of course set these properties again later through our 'set' methods.

Now let's take a look at our complete class with all the code sections together:

<php

class Person {

private $name;

private $age;

private $gender;

private $job;

function __construct($new_name, $new_age, $new_gender, $new_job) {

$this->name = $new_name;

$this->age = $new_age;

$this->gender = $new_gender;

$this->job = $new_job;

}

public function set_name($new_name) {

$this->name = $new_name;

}

public function get_name() {

return $this->name;

}

public function set_age($new_age) {

$this->age = $new_age;

}

public function get_age() {

return $this->age;

}

public function set_gender($new_gender) {

$this->gender = $new_gender;

}

public function get_gender() {

return $this->gender;

}

public function set_job($new_job) {

$this->job = $new_job;

}

public function get_job() {

return $this->job;

}

}

?>

Now that we have our class created, we need to create a 'Person' object.

We first do this by making sure we include the class file so PHP knows what we are talking about when we call the constructor.

With access to our class code, we can create a new object. We do this simply by assigning a variable to a new object. The new keyword signifies that we are declaring a new object. And we tell it that we want a new Person object.

At the same time, we pass in the four values we will assign to our Person. We pass these in just the same as we would to a normal PHP function. These are in the order they are set out in the class constructor - name, age, gender and job.

include_once("person.php");

$new_person = new Person("Mike", "28", "male", "mechanic");

So we have created a new Person. His name is 'Mike', he is 28 years old, he is a male and he is a mechanic.

Now that we have the object created, if we wanted to access the properties we would use the 'get' methods we set up in the class.

To print his name on the screen we would use:

echo $new_person->get_name();

This calls the get_name() method which returns the object's name property. If we wanted to get Mike's age we would call the object's get_age() method.

If we want to update a property after we have created an object, we call the property's 'set' method. For example, if we want to update Mike's occupation from mechanic to supervisor, we would call set_occupation() and pass in the new occupation, like so:

$new_person->set_occupation("supervisor");

This would set the occupation property to 'supervisor'.

And that is a basic and brief introduction to the very powerful and extensive methodology of Object Oriented Programming in PHP. You should take the skills and knowledge learned here and experiment with them and see what you can come up with. Other complex PHP functions can be called and logic performed inside the class, making the objects much more versatile than the basic Person object we have studied here.

When creating several objects, which is more common than not, storing them in an array is a great way to go. Then you can simply cycle through the array and access each object in turn. So if you had a list of club members for example, each member could be a separate object (similar to our Person object here) and all be held in a 'members' array.

There are far too many possibilities for us to list or even start exploring here.

ferrari_chris

rashed says:

2012-02-24 11:40:21

Great tutorial


Add your comment on this article below:

Sorry, there's an error with your form entries. We really appreciate your comment, so please try again.

Form submitting now...

Name:

Website:

Email address (not displayed):

Enter your comment below: