06.Object-Oriented Programming in PHP
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
- Class → template for objects
- Objects → instance of classes
Attributes and Methods
- Attributes → identity / characteristic that is inside a class
- Methods → behavior / activity that could be done by a class
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 getName() {
return $this->name;
}
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 objects
$warrior = new Character("Baraka"); from class easily now
$ninja = new Character("Sub Zero", 300);
<?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
- Inheritance lets us to reuse a part of a program
- Inheritance makes program creation easier
- Has a parent-child relationship
- Parent class is called as the “super” class
- Children class is called as the “sub” class
- If a class “inherits” from a superclass, every public / protected attributes and
methods are accessible
- By doing this, we do not need to re-create a certain part of our program
Case
In a game, we have multiple options for weapons like sword, axes, guns, bows, etc
1. Name
2. Damage
We could utilize the inheritance to simulate how the weapon is constructed in a game
$saber->setDamage(200);
$saber->setSharpness(1000);
echo($saber->getInfo());
?>
Polymorphism
- Polymorphism means: many shapes
- Ability of an object to have different implementations but sharing the same interface
(in this case, functions / methods)
- Polymorphism could be simulated by using abstract class / interface
- Using the concept of method overriding / overloading
- Overriding → replace the implementation of the method / function with the same name
- Overloading → replace the implementation of the method / function with additional parameters
Overriding vs Overloading
Overloading: Overriding:
$bow->setAmmo(3);
$gun->setAmmo(100);
Take a look how we define the
$bow->shoot();
functions with same name, but for a
for ($i = 1; $i <= 10; $i++) { totally different object
$gun->shoot();
}
?>
PHP Namespace
- Qualifiers to organize files in a project / application
- Using namespace, we could differentiate classes with same name but located in
different folder
Example of a case:
1. C:\xampp\htdocs\Office\employee.php
2. C:\xampp\htdocs\Factory\employee.php
Question: When you create a project, how would be be able to differentiate between
employee.php in Office or in Factory folder?
Example: employee.php inside Factory folder
<?php
namespace Factory; Add namespace at the beginning of
the file to show this is in the area /
class Employee { domain of Factory
private $name;
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
Create a class that simulates the employee data in a company.
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/