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

Understanding PHP Classes and Objects

Follow Roger Facebooktwittergoogle_pluslinkedinrss

In object oriented programming, or OOP, classes are like factories (real-world factories, not OOP factories. I’ll write more on OOP Factories in the future). These factories define and construct objects. In learning OOP I usually saw classes described as blueprints for objects. But, they are more than just a blueprint; classes do construct objects. In PHP, we create a class using the class keyword (keywords implement built-in PHP functionality). Here is an example of a PHP class:

<?php

class Person {

     //define person here
}

In order to create an object from our class, or instantiate our class, we set a variable equal to a new instance of our class. We do this by using the new keyword. An instance is an object created from a class.

$jill = new Person();

Part of the power of OOP lies in the fact that we can have more than one instance of a class.

$jack = new Person();

We now have 2 instances of our Person() class – $jill and $jack. Both $jill and $jack are objects built from the Person() class. Let’s give our class a property – properties are variables within the class.

<?php

class Person {

     public $gender = 'male';
}

Don’t be concerned with public, yet. By using public we are setting where our $gender variable can be seen. This is known as encapsulation, which I’ll cover in a later post. For now, let’s go with a simplified definition and say that using public means that the property can be accessed outside of the class. Let’s put everything we have so far together in one simple script.

<?php

//set up our class
class Person {

     public $gender = 'male';
}

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

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

In the code above, I introduced a couple of new things. var_dump is an internal (built-in) PHP function that does pretty much what its name says – it dumps, or outputs, the values of what is inside the parentheses to the screen. The variables inside the parentheses are called arguments. The notation objectName->variableName is how you access the value of a property of an object. Above, $jack->gender and $jill->gender both equal ‘male’, since we set the property $gender = ‘male’ in our class. The two var_dump statements above will both output ‘male’.

It is perfectly fine to hard code the value of a property. But, in our case, it is not what we want. For example, in our $jill object we created above we likely don’t want $gender to be ‘male’. Likely, we want $gender to be ‘female’. How do we do that? I’ll cover that in my next post, part 2.

Share with your social networks Facebooktwittergoogle_plusredditpinterestlinkedinmail