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


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>