PHP Classes and Objects, part 2

Follow Roger Facebooktwittergoogle_pluslinkedinrss

This is part 2 of a series. Click here for Part 1

OK. We have our Person object, and we have 2 instances of it set to $jack and $jill. We have a $gender property set to male. But, our $jill object (instantiated from the Person class) should be female. We need the ability to set the gender when an object is instantiated from the class.  This can be done with a constructor method. A method is nothing more than a function within the context of a class.

A constructor method must be named __construct() – note that is 2 underscores preceding the word “construct”. The __construct() method is not required. If it does exist on the class, when am object is instantiated from that class, the method is automatically called.

public function __construct()
{
     //constructor code here
}

For our example class Person, we want the ability to set the $gender property each time we instantiate the class. We do this through the __construct() method by assigning an argument to the method. Arguments are optional on the __construct() method. Recall how we placed an argument in the var_dump() method. We will do the same, but pass a string instead of a variable when we instantiate the objects.

$jack = new Person('male');
$jill = new Person('female');

Now we have to handle the argument in our __construct() method. In the Person() class remove the value assignment of our $gender property. Then accept the argument in the __construct() method.

<?php

//set up our class
class Person {

     public $gender;
     
     public function __construct($input)
     {
          //do something with $input
     }
}

When an object is instantiated $input will be set to the value of the argument supplied in the instantiation call. We need to set the $gender property to that value.

$this->gender = $input;

I had difficulty getting my head around the concept of $this. From what we have already seen, you know that the code above is accessing the gender property on the $this object. Simply put, $this is a variable that stands in for the instantiated object. In our example we have instantiated $jill and $jack as separate Person() objects. So, on the $jill object $this refers to $jill. And, on the $jack object $this refers to $jack. The same would be true of any object instantiated from the Person() class.

Let’s pull everything together into one short script.

<?php

//set up our class
class Person {

     public $gender;

     public function __construct($input)
     {
        $this->gender = $input;
     }
}

//create 2 objects by instantiating our class twice
$jack = new Person('male');
$jill = new Person('female');

var_dump($jack->gender);
var_dump($jill->gender);

When we instantiate the object as $jack we pass the argument as ‘male’. The class accepts the argument in the __construct() method as $input. The method then sets the class property $gender as the value of $input.

The code above will output:
male
female

Hopefully, this lesson helps you understand PHP objects and classes, how they relate to each other, and gives you a start toward building your own.

Check back for future PHP lessons. Be sure to follow me on Twitter @RogerCreasy, or click the icon above.

Share with your social networks Facebooktwittergoogle_plusredditpinterestlinkedinmail


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>