Object Oriented PHP Tutorials – Object Functions and Variables

Variables and Functions Types

There are several types of functions and variables that classes can have, but I will only cover one type for now; public.

The other types  are

  • static
  • private
  • protected

The reason of why am not going to go over these types is because they won’t make much sense to you until we start talking about inheritance: letting a class inherit members from another one. I will tell you about static types in my next post though.

Public Functions

The syntax for declaring functions inside a class is the following.

[type] function [name] ([parameters])
{
   // function code
}

By default, all functions are public unless noted otherwise.

Assume that you have the following script.

class Human
{
   function __construct($name,$dateOfBirth)
   {
        echo 'A human named '.$name.' was born at unix time: '.$dateOfBirth;
   }
}

$person=new Human('Matt',time()); // create object $person from class Human

Let’s give some human qualities to this object-person. The first thing I can think of is the function of speaking , so let’s do that.

class Human{

   function __construct($name,$dateOfBirth)
   {
        echo 'A human named '.$name.' was born at unix time: '.$dateOfBirth;
   }

   public function speak($words)
  {
    echo $words;
   }

}

Remember that the “public” keyword is optional.

Now all of the objects made from the class Human can talk. The syntax for using an object’s function is the following.

$objectName->functionsName();

Try this after your class code.

$person=new Human('Matt',time());
$person->speak('I can talk!');

Public Object Variables

The problem with our current constructor is that we don’t save our human’s name or date of birth for later use. Object variables take care of this problem by letting us store values in variables that pertain only to that object. There are also class variables, which I will cover in my next post, that store variables which can be used by ALL objects made from that particular class.

There are two ways to declare public object variables. The first way is to explicitly precede a variable’s name with the word “public” and refer to that variable thereafter, inside the class, like this

public $variableName;
// you can now refer to this variable with $this->variableName;

The second way is to just begin using the variable the same way as above.

$this->variableName;

Examples

The following two snippets of code do the same thing, they will store the constructor’s variables $name and $dateOfBirth in objects variables.

example1

class Human{

public $name;
public $dob;

   function __construct($name,$dateOfBirth)
   {
       $this->name=$name;
       $this->dob=$dateOfBirth;
        echo 'A human named '.$name.' was born at unix time: '.$dateOfBirth;
   }

   public function speak($words)
  {
    echo $words;
   }

}

example 2

class Human{

   function __construct($name,$dateOfBirth)
   {
       $this->name=$name;
       $this->dob=$dateOfBirth;
        echo 'A human named '.$name.' was born at unix time: '.$dateOfBirth;
   }

   public function speak($words)
  {
    echo $words;
   }

}

The $this-> Operator

Think of $this like this: if you want to use a class’ function or variable inside itself, since you don’t have an object yet, you refer to it as $this.

Out side of the class though, once you have made an object a class, you use the object name. Let’s show a objects $name and $dob variable.

Try this:

$person1=new Human('Mike',time());
$person2= new Human('Joseph',time());

echo $person1->name; // shows Mike
echo $person2->dob; // will show Joseph's date of birth (in unix time)

Another way to show an object’s information would be to make function inside the class that shows each variable.

public function showName()
{
echo $this->name;
}

public function showDob()
{
echo $this->dob;
}

So now we can do this:

$person1=new Human('Mike',time());
$person2= new Human('Joseph',time());

echo $person1->showName(); // shows Mike
echo $person2->showDob(); // will show Joseph's date of birth (in unix time)