OOP IN

What is OOP?

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.

OOP Vocabulary

  • Classes (The description or blueprint for creating an object.)
  • Object ( a thing created from a class.)
  • Property (a variable on a object, attribute)
  • Method (a function on a object, behavior)
  • Instanciate (the action of creating an object.)
  • Encapsulation
  • Inheritance 
  • Abstraction
  • Polymorphism

Classes and Objects

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

Inheritance

  • 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

Language Constructs

Interface

  • A interface is a way of describing the behaviors  of an object.
  • Classes can "implement" an Interface.
  • If a class implements an interface it must build out the functionality for all methods defined on the interface.
<?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

Abstract Class

  • An Abstract class is a class that cannot be instanciated only extended.
  • An abstract class exists so that multiple child classes can extended and the methods implemented in the Abstract class can be re used.
  • The main purpose for the abstract class is for subtyping.
  • abstract classes must be declared using the "abstract" keyword before the class keyword.
<?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';
    }
}



Encapsulation

  • Encapsulations is the hiding of the internal workings of an object.

 

  • Packing up data properties and behaviors into a single unit.

 

  • Whenever we create a class we are doing just this.

 

<?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');
        }
    }
}

Abstraction

  • Abstraction and encapsulation are complementary concepts:

 

  • Abstraction focuses on the observable behavior of an object.

 

  •  Encapsulation focuses upon the implementation that gives rise to this behavior.

 

  • Encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do not contribute to its public Interface.

Polymorphism

  • Polymorphism is a complex word for a pretty simple concept.

 

  • Polymorphism etymology just means is greek for "Many Forms"

 

  • Polymorphism is when we have different objects that share the same interface but the implementation the methods is internally different.

OOP in PHP

By Juan Manuel Torres

OOP in PHP

  • 1,328