Learn Object Oriented PHP Step by Step – Classes and Constructors

Welcome to part one of this series, in order to learn this method of programming in PHP and other languages you must be familiar with functions, which my last post covers.

Naming and Defining Classes

You can think of classes as sets of functions and variables that work together to achieve one goal, these functions and variables are called class members. The steps to defining and naming classes are these.

  • Type the keyword class
  • Type the class’ name
  • Type an opening and closing curly bracket

A class’ name cannot begin with a number and it cannot have symbols other the underscore. In other words just like functions.

Example:

class ClassName
{
 // class members go here
}

As with most things, PHP doesn’t have one naming convention, but we like to use camelCase or an underscore to separate_words in a class’ name.

Class Objects

In order to use members of a particular class you need to initialize, make an object of or instantiate the class, these three things mean the same thing.

An object has the same naming conventions that variables do and you even use the dollar sign “$” just like you would do with variables.

Once an object is created it has access to the members of the class from which it was made.

How to Make Objects

The steps to making an object are.

  • Type the name of the object you will use
  • Type and equals sign “=”
  • Type the keyword “new”
  • Type the class’ name as if you were calling a function

Example: Let’s make a class called Human and initialize it. We will use this class for the next section, so save it.

class Human
{
  // class members will go here
}

$person1=new Human();  // this where we initialize a class

If you run this script nothing will happen, that’s because we don’t have any class members that do anything yet.

Class Constructors – A Special Type Of Function

In most of the tutorials and PHP books I’ve read the author explains constructors last, but I think you will get what they are better if I explain them first.

Constructors are a special type of function that runs automatically as soon as an object is made. In our previous example that would be this next line.

$person1=new Human(); // the constructor is executed as soon as this line runs

If the constructor was executed at that line, why didn’t I see anything? That’s a good question, classes have a default constructor that doesn’t do anything, the only reason for you to have a constructor in your class is if you want to do something upon initialization of the class.

Before I show you how to declare a constructor inside a class here is a list of things you should know about constructors.

  • Constructors are functions – Because they are functions they can take parameters
  • Unlike with other languages, classes in PHP can only have one constructor.

Defining Constructors, Their Naming Conventions and Playing God

There are only two naming conventions for constructors depending on which version of PHP you are using.

Before PHP5 and PHP5

The naming convention was that the constructor had to have the same name as the class.

After PHP5

If you are running PHP5 or above, you can use the old style or you can use “__construct” as the name for the constructor.

There are TWO underscores before the word “construct”

Declaring a Constructor Example

class Human
{
   function Human() // php4 and optional php5 style
  {
     echo "A human was born";
   }

  function __construct() // php5 and above style
 {
   echo "A human was born";
 }

}

Am going to stick with PHP5’s style for the rest of the series, so my class so far is this.

class Human
{

  function __construct() // php5 and above style
 {
   echo "A human was born";
 }

}

And when I run the next line, I will see the text “A human was born” .

$person1=new Human();

I can have more than one object per class, so the following lines will display “A human was born” twice.

$person1=new Human();
$person2=new Human();

Constructors With Parameters

Most parents have names for their kids as soon as they are born so why don’t we name each of our humans at the moment they are born ( or in this case instantiated ).

Let’s change our previous constructor to be the following.

class Human
{

  function __construct($name)
 {
   echo "A human named ".$name." was born";
 }

}

Hurray! Now each human will have a name.

$person1=new Human('John'); // will display "A human named John was born "

Like functions, constructors can take more than one parameter.

class Human
{

  function __construct($name, $time) // php5 and above style
 {
   echo "A human named ".$name."was born at unix time: ".$time;
 }

}

$person1=new Human('John',time()); // A human named John was born at unix time: 1268627707

The Problem With These Constructors

The problem with the constructors we just made are that our objects, humans, loose their identities because we are not storing their names and birthdays in variables that we can later use with other functions. These variables am referring to are object variables, there are also other types of variables called class variables, class variables could be used to keep track of how many humans have been “born” for example.

I will cover variables and function in my next post, in the mean time don’t forget to share this article and subscribe.