Connection failed: could not find driver

PHP development


Lesson 10

OOP - Object Oriented Programming


What is an Object?

An Object in programming is, just like in the real world, an entity that has behaviour in the form of methods and has properties.

As an example we take a car. A car has bahaviour like going forward, going backward, trun left, turn right, stopping, etc.
Its properties are doors, a steer, a motor, wheels, windscreens, windscreen whipers, a trunk, etc.

Every car has this behaviour and properties but some behaviour may be different, from others. The same for properties. Some cars have 2 doors and some have four. or some cars have automatic transmission and some cars manual transmission. or different colors. And every car has it's own unique serial number.

So every Object, in this case a car, has its uniqueness. But still all car objects are part of the same class: Car.

In programming PHP

In PHP, before we can create an object we have to define it's class. From that class we can create objects.

Defining the class:


<?php

class Car
{

}

Adding properties to the class


<?php

class Car
{
	public $color;
	public $doors;
	public $steer;
	public $wheels;
	public $transmission;
	public $motor;
	
	private $id;
	private $serialNumber;
	private $speed;
	
}

Adding behaviour to the class


<?php

class Car
{
	public $color;
	public $doors;
	public $steer;
	public $wheels;
	public $transmission;
	public $motor;
	
	private $id;
	private $serialNumber;
	private $speed;
	
	public function __construct($id = null)
	{
		if(null !== $id){
			$this->get();
		}
	}
	
	public function setSpeed($speed = 0)
	{
		$this->speed = $speed;
	}
	
	public function getSpeed(){
		return $this->speed;
	}
	
	public function start($key = false)
	{
	
		if(!$key){
			echo 'we cannot start, put in the key';
		}else{
		 	echo 'car is started';
	 	}
		
	}
	
	public function drive()
	{
		
	}
	
	public function break()
	{
	
	}
	
	public function stop()
	{
	
	}
	
	private function setSerialNumber($serialNumber){
		$this->serialNumber = $serialNumber ; 
	}
	
	public function getSerialNumber(){
		return $this->serialNumber
	}	
	
	/**
	 *  save to database
	 *
	 */
	public function save(){
		$serialnumber = Serialnumber::getNew();
		$this->setSerialNumber(#serialnumber);
		
		.... other code to save to database
	}
	
	/**
	 *  retrieve from database
	 *
	 */
	public function get(){
		
	}
	
} 

Creating a new object (and save to the database)


$car = new Car();
$car->color = 'red';
$car->steer = 'left';
$car->doors = ['FL-3345', 'FR-3345', 'BL-3345', 'BR-3345'];
$car->wheels = ['15"','15"','15"','15"'];
$car->transmission = 'automatic';
$car->motor = 'QR23V6150',


//save the object to the database
$car->save();

echo $car->speed;

creating an object from database



$car2 = new Car(202);

Using the object


$car->start();
$car->drive();

Question: How would we get the speed?

Question: How to stop the car?

Question: How do we know what color $car2 has?

Question: How do we get the serial number form $car2?

Homework

as you can see in the $car object, we have added an array to the property 'doors':


$car->doors = ['FL-3345', 'FR-3345', 'BL-3345', 'BR-3345'];

This suggests that we have 4 different doors with different type numbers.
FL for front-left
FR for front-right
BL for back-left
BR for back-right

Create a class 'Door'. Think about what properties a car door has and add those to the class.

Also, think about the behaviours of a car door and create methods (functions) for these behaviours. pay special attention to the serial numbers as they should be private properties

After you have created the class, create a new object $door; After you have created $door, unlock and lock $door. Also turn down the $door's window.

success!