Object Oriented PHP for Beginners: Steps 6 – 11

Step 6:

The ‘$this’ variable

You probably noticed this line of code:

$this->name = $new_name;

The $this is a built-in variable (built into all objects) which points to the current object. Or in other words, $this is a special self-referencing variable. You use $this to access properties and to call other methods of the current class.

function get_name() {
	return $this->name;
}

Note: This may be a bit confusing for some of you … that’s because you are seeing for the first time, one of those built in OO capabilities (built into PHP5 itself) that automatically does stuff for us.

For now, just think of $this as a special OO PHP keyword. When PHP comes across $this, the PHP engine knows what to do.

… Hopefully soon, you will too!

Step 7:

Use your class in your main PHP page. (index.php)

You would never create your PHP classes directly inside your main php pages – that would help defeat the purposes of object oriented PHP in the first place!

Instead, it is always best practice to create separate php pages that only contain your classes. Then you would access your php objects/classes by including them in your main php pages with either a php ‘include’ or ‘require’.

<?php include("class_lib.php"); ?>

Note: Notice how we haven’t done anything with our class yet. We will do that next.

Step 8:

Instantiate/create your object

Classes are the blueprints/templates of php objects. Classes don’t actually become objects until you do something called: instantiation.

When you instantiate a class, you create an instance of it … thus creating the object.

In other words, instantiation is the process of creating an instance of an object in memory. What memory? The server’s memory of course!

<?php include("class_lib.php"); ?>
<?php 
	$stefan = new person();
?>

Note:  The variable $stefan becomes a handle/reference to our newly created person object. I call $stefan a ‘handle’, because we will use $stefan to control and use the person object.

If you run the PHP code now, you will not see anything displayed on your  pages. The reason for this, is because we have not told PHP to do anything with the object we just created.

Step 9:

The ‘new’ keyword

To create an object out of a class, you need to use the ‘new’ keyword.

When creating/instantiating a class, you can optionally add brackets to the class name, as I did in the example below. To be clear, you can see in the code below how I can create multiple objects from the same class.

… From the PHP’s engine point of view, each object is its own entity.  Does that make sense?

<?php include("class_lib.php"); ?>
<?php 
	$stefan = new person();
	$jimmy = new person;
?>

Note:   When creating an object, be sure not to quote the class name.

For example:

$stefan = new 'person';

… will give you an error.

Step 10:

Set an objects properties

Now that we’ve created/instantiated our two separate ‘person’ objects,  we can set their properties using the methods (the setters) we created.

Please keep in mind that though both our person objects ($stefan and $jimmy)  are based on the same ‘person’ class, as far as php is concerned, they  are totally different objects.

<?php include("class_lib.php"); ?>
<?php 
	$stefan = new person();
	$jimmy = new person;
	$stefan->set_name("Stefan Mischook");
	$jimmy->set_name("Nick Waddles");
?>

Step 11:

Accessing an object’s data

Now we use the getter methods to access the data held in our objects … this is the same data we inserted into our objects using the setter methods.

When accessing methods and properties of a class, you use the arrow  (->) operator.

<?php include("class_lib.php"); ?>
<?php 
	$stefan = new person();
	$jimmy = new person;
 
	$stefan->set_name("Stefan Mischook");
	$jimmy->set_name("Nick Waddles");
 
	echo "Stefan's full name: " . $stefan->get_name();
	echo "Nick's full name: " . $jimmy->get_name(); 
?>

Note: The arrow operator (->) is not the same operator used with associative arrays: =>.

Congratulations, you’ve made it half way through the tutorial! Time to take a little break and have some tea … OK, maybe some beer.

In a short period of time, you’ve:

  • Designed a PHP class.
  • Generate/created a couple of objects based on your class.
  • Inserted data into your objects.
  • Retrieved data from your objects.

Not bad for your first day on the OO PHP job.

If you haven’t already, now is a great time to write out the code and watch  it in action in your own PHP pages.

Questions?

Just post them on the PHP forum.

To Top