Object Oriented PHP for Beginners: Steps 1 – 5

For this tutorial, you should understand a few PHP basics: functions, variables, conditionals and loops.

To make things easy, the tutorial is divided into 22 steps.

Step 1:

First thing we need to do is create two PHP pages:

  • index.php
  • class_lib.php

OOP is all about creating modular code, so our object oriented PHP code will be contained in dedicated files that we will then insert into our normal PHP page using php ‘includes’.

In this case, all our OO PHP code will be in the PHP file:

  • class_lib.php

OOP revolves around a construct called a ‘class’. Classes are the cookie-cutters / templates that are used to define objects.

Step 2:

Create a simple PHP class (in class_lib.php)

Instead of having a bunch of functions, variables and code floating around willy-nilly, to design your php scripts or code libraries the OOP way, you’ll need to define/create your own classes.

You define your own class by starting with the keyword ‘class’ followed by the name you want to give your new class.

<?php 
	class person {
 
	}
?>

Note: You enclose a class using curly braces ( { } ) … just like you do with functions.

Step 3:

Add data to your class

Classes are the blueprints for php objects – more on that later. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: ‘object’.

When you create a variable inside a class, it is called a ‘property’.

<?php 
	class person {
		var $name;
	}
?>

Note: The data/variables inside a class (ex: var $name;) are called ‘properties’.

Step 4:

Add functions/methods to your class

In the same way that variables get a different name when created inside a class (they are called: properties,) functions also referred to (by nerds) by a different name when created inside a class – they are called ‘methods’.

A class’s methods are used to manipulate its own data / properties.

<?php 
	class person {
		var $name;
		function set_name($new_name) {
			$this->name = $new_name;
		}
		function get_name() {
			return $this->name;
		}
	}
?>

Note: Don’t forget that in a class, variables are called ‘properties’ and functions are called ‘methods’.

Step 5:

Getter and setter functions

We’ve created two interesting functions/methods: get_name() and set_name().

These methods follow a common OOP convention that you see in many languages (including Java and Ruby) – where you create methods to ‘set’ and ‘get’ properties in a class.

Another convention is that getter and setter names should match the property names.

<?php 
	class person {
		var $name; 
		function set_name($new_name) { 
			$this->name = $new_name;  
 		}
 
   		function get_name() {
			return $this->name;
		}
	} 
?>

Note: Notice that the getter and setter names, match the associated property name.

This way, when other PHP programmers want to use your objects, they will know that if you have a method/function called ‘set_name()’, there will be a property/variable called ‘name’.

Questions?

Just post them on the PHP forum.

To Top