06.Object-Oriented Programming in PHP
06.Object-Oriented Programming in PHP
Programming
Object-Oriented Programming in PHP
Object-Oriented Programming
- Models a program into objects
- Use “class” to create a template for objects
- Four pillars of OOP:
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Reasons of adapting OOP
- Clear and more structured application
- Prevent repetition in creating parts of program (DRY → Don’t Repeat
Yourself)
- Create reusable application
Class and Object
function getName() {
return $this->name;
}
function getHealth() {
return $this->health;
}
function getHit($damage=1) {
$this->health -= $damage;
}
}
?>
Example: main.php
<?php
require_once("character.php");
// create object
$warrior = new Character();
// set attributes
$warrior->name = "Baraka";
$warrior->health = 100;
function getHealth() {
return $this->health;
}
function getHit($damage=1) {
$this->health -= $damage;
}
}
?>
Example: main.php (changed)
<?php
require_once("character.php");
// create objects We could instantiate
$warrior = new Character("Baraka"); objects from class easily
$ninja = new Character("Sub Zero", 300); now
<?php
class Character {
public static $characterCount = 0;
public $name;
public $health;
<?php
require_once("character.php");
// create object
$warrior = new Character("Baraka");
$ninja = new Character("Sub Zero", 300);
<?php
require_once("character.php");
// create object
$warrior = new Character("Baraka");
$warrior->setName("Kung Lao");
echo("Change name to {$warrior->getName()}");
?>
Inheritance
In a game, we have multiple options for weapons like sword, axes, guns,
bows, etc
1. Name
2. Damage
<?php
require_once("sword.php");
$saber = new Sword("Saber", 100, 100);
echo($saber->getInfo());
$saber->setDamage(200);
$saber->setSharpness(1000);
echo($saber->getInfo());
?>
Polymorphism
Overloading: Overriding:
- Known as Static / Compile-Time - Known as Dynamic / Run-Time
polymorphism polymorphism
- Methods are located in the same - Methods are located in the
class, with same name but different class (children) with
different parameters same name and same
parameters
<?php
abstract class Projectile {
protected $ammo;
$bow->setAmmo(3);
$gun->setAmmo(100);
Take a look how we define the
$bow->shoot();
functions with same name, but
for ($i = 1; $i <= 10; $i++) { for a totally different object
$gun->shoot();
}
?>
PHP Namespace
Example of a case:
1. C:\xampp\htdocs\Office\employee.php
2. C:\xampp\htdocs\Factory\employee.php
class Employee {
private $name;
<?php
require("Factory/employee.php");
require("Office/employee.php");
echo($empOffice->getName() . "<br/>");
echo($empOffice->getSalaryInfo() . "<br/>");
echo($empFactory->getName() . "<br/>");
echo($empFactory->getSalaryInfo() . "<br/>");
?>
Exercise
1. Name
2. ID
3. Gender
4. Salary
An employee could:
https://www.w3schools.com/php/php_oop_classes_objects.asp
https://www.w3schools.com/php/php_oop_inheritance.asp
https://www.w3schools.com/php/php_oop_classes_abstract.asp
https://www.w3schools.com/php/php_oop_static_methods.asp
https://www.php.net/manual/en/language.namespaces.rationale.php
https://www.codecademy.com/resources/blog/what-is-inheritance/
https://stackify.com/oop-concept-polymorphism/