OOP is a design philosophy of grouping data and behavior into self sustainable and reusable representations of concepts.
(Object Oriented Programming)
In OOP linear procedural statements are replaced by the instances of objects that can be used to call those specific behaviors.
A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object.
A class is the blueprint from which the individual objects are created. A Class is composed of three things: a name, properties, and methods.
An object can be considered a "thing" that can perform a set of related behaviors.
<?php
// The blueprint.
class Person
{
// $name is a property on the class.
protected $name;
// The __construct is a "magic" method called whenever a new Person is created.
public function __construct($name)
{
$this->name = $name;
}
// greet is a method of the Person Class.
public function greet()
{
return "Hello my name is " . $this->name;
}
}
// $elijah is a instance of the Person Class. The actual "object".
$elijah = new Person('Elijah');
// Calling the greet method on the newly defined instance of Person.
echo $elijah->greet(); // outputs Hello my name is Elijah
The ability to create a new class from an existing class by extending it.
Classes can extend the behaviour and properties of other classes by using the "extends" keyword.
Classes can only extend one class.
<?php
class Person
{
protected $name;
public function __construct($name)
{
$this->validateName($name);
$this->name = $name;
}
public function greet()
{
return "Hello my name is " . $this->name;
}
protected function validateName($name)
{
if(null == $name){
throw new \InvalidArgumentException('Name must not be null');
}
}
}
class User extends Person
{
protected $email;
public function __construct($name, $email)
{
parent::__construct($name);
$this->email = $email;
}
}
$elijah = new User("Elijah", "elijah@elijah.com");
echo $elijah->greet(); // outputs Hello my name is Elijah
<?php
interface Creature
{
public function speak();
public function move();
public function greet();
}
class Human implements Creature
{
public function speak()
{
return "Human speech";
}
public function move()
{
return "walk on two legs";
}
public function greet()
{
return "Extend hand."
}
}
$creature = new Human();
$creature->speak(); // outputs "Human speech"
class Dog implements Creature
{
public function speak()
{
return "Roof Roof";
}
public function move()
{
return "gate on 4 legs";
}
public function greet()
{
return "Extend tongue."
}
}
$creature = new Dog();
echo $creature->speak(); //outputs Roof Roof
<?php
abstract class AbstractHuman implements Creature
{
protected $firstName;
protected $lastName;
public function __construct($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->gender = $gender;
}
abstract public function speak();
abstract public function move();
public function greet()
{
return "Extended hand";
}
}
class Woman extends AbstractHuman
{
public function move()
{
return "I move with grace";
}
public function speak()
{
return 'Hello my name is ' . $this->firstName . ' ' . $this->lastName . 'in a high pitch voice';
}
}
class Man extends AbstractHuman
{
public function move()
{
return "I move with long stride";
}
public function speak()
{
return 'Hello my name is ' . $this->firstName . ' ' . $this->lastName . 'in a low deep voice';
}
}
<?php
class Person
{
protected $name;
public function __construct($name)
{
$this->validateName($name);
$this->name = $name;
}
// Here we validate the name. This is an internal detail.
protected function validateName($name)
{
if(null == $name){
throw new \InvalidArgumentException('Name must not be null');
}
}
}