This is continuation of Part 1 of Object Oriented Programming in PHP. In this tutorial we would cover the following:
1. Creating Objects
Assuming we have a class representing a car a shown below, how do we create an object from it?
<?php class Car { var $make; var $model; var $speed; function increaseSpeed($newSpeed){ $this->speed = $speed + $newSpeed; } function getMake() { return $this->make; } function setMake($newMake){ $this->make = $newMake; } } ?>
With this class, we can then create objects using the the new operator.
The code below creates 3 objects: a Mustang, a Prado and Passat.
$mustang = new Car; $prado = new Car; $passat = new Car;
These three objects are instances of Car. However, they are independent of each other as we would see.
2. Accessing Member Functions
After creating the object, you can then access the member functions of that object. As mentioned before, you will be able to access the member variables using the member functions.
$mustang->setMake("Ford");
$prado->setMake("Toyota");
$passat->increaseSpeed(20);
The codes above assigns or alters the values to the member attributes of the instances and does not return anything. After you have assigned the values you than then retrieve them.
For example you can retrieve and display the speed and make using:
echo $mustang->getMake(); echo $prado->getMake(); echo $passat->getMake();
3. Constructor Functions
As mentioned previously, constructor function are a special type of function called automatically whenever a new object is instantiated. One thing that happens during this time is that member variables are initialized.
In PHP, you can define a constructor using the _construct() function. In the following example, we create a constructor for a Car. This constructor will initialize the make, model and speed.
function __contruct($param1, $param2, $param3){
$this->make = $param1;
$this->model = $param1;
$this->speed = $param1;
}
With this constructor, now when you create a new object, you can provide the member variables at the same time as parameters.
$mustang = new Car("Toyota Corola", "2019", 180);
$prado = new Car("Ford Focus", "2020", 260);
$passat = new Car("Nissan Patrol", "2018", 200);
So with this code, you don’t need to assign the member variables separately.
Destructor – just like constructors, you can also define a destructor function using the __destruct(). With it you can specify what happens when the object goes out of scope. For instance, release certain resources.
4. Implementing Inheritance
You can define a class to inherit from an existing class in PHP. To achieve that, use the syntax below;
class Child extends Parent{
//variables and functions
//...
}
When a class derives from another class, the following occurs:
- the child class automatically inherits all the member variables of the parent
- the child class also automatically inherits all the member functions of the parent
The example below is a class Taxi, that inherits from the Car class.
class Taxi extends Car{
var $fare;
function setFare($param){
$this->fare = $param;
}
function getFare(){
echo $this->fare;
}
}
You can see that the taxi inherits from Car. Moreover, it also has two additional member functions and a member variable to itself.
5. Public, Private and Protected
By default, the member variables and functions of a class are public. This means they can be accessed by derived classes as well as from outside the class. However, you can limit the accessibility of these members using the private or protected keyword.
Private Members
Member functions or variables declared as private are accessible only within the class they are declared. Therefore, they are no accessible to derived classes
Protected Members
Protected member functions or variables are accessible in the class they are declared as well as to derived classes.