Security, Tech & Programming
PHP OOP Concepts CheatSheet

PHP OOP Concepts CheatSheet

What is a class property

A class property is just like a variable, but declared inside a class (at base level, not inside a function / method). For example:

class Property {
    // this variable is a property
    $one = 'some value';

    public function two(){
        // this variable is not called property, simply local variable
        $three = 'value of three';
    }
}

What is a class method

Method is simply a function inside a class. For example:

class Method {
    // this function inside class will be called method
    public function one(){
        // any logic here
    }
}

PHP OOP Encapsulation

Encapsulation simply means making the data private and not directly accessible from outside the class. So that we can deliver it through a provided endpoint method. For example:

class Encapsulation {
    // private properties are not accessible from outside
    private $one = 'some value';

    public function getOne() {
        // do some logic check here, & if true then return $one
        return $this->one;
}

PHP OOP Aggregation

Aggregation simply means that the class calls another class to create an object when it is initiated. Aggregation is different from composition (more below). For example:

class Aggregator {
    private $internal_obj;

    public function __construct() {
        $this->internal_obj = new OutsideClass;
    }
}

PHP OOP Composition

OOP composition is different from aggregation (above & below). Composition uses another class object too, but that object has to exist before the class is called. For example:

class Composition {
    private $external_obj;

    public function __construct(B $external_obj) {
        $this->external_obj = $external_obj;
    }
}

Difference between OOP composition & aggregation

Full names of both concepts are:

  • Composition: composition aggregation
  • Aggregation: basic aggregation

Both of them use external class objects. However, composition means that the object has to be available and passed to the class before it is created, while aggregation does not require the object to be already existing. Aggregation creates the new object from the required external class itself.

PHP OOP Abstract Class & methods

An abstract can not create an object directly. It can only be used as a parent class from which child classes can inherit from.

The abstract methods only declare the type and details of the method, but not the code implemented inside of it. The child class will handle it.

An abstract class can have abstract methods as well as normal methods. A child class can overwrite the normal methods of parent class too.

abstract class Parent
{ 
    // This is an abstract function 
    abstract public function abstractMethod(); 
      
    // This is not an abstract function 
    // child can use this via: $this->nonAbstractMethod()
    public function nonAbstractMethod() 
    {
        echo "hello world from abstract class!"; 
    } 
}

class Child extends Parent {
    // child class has to implement abstract methods by compulsion
    public function abstractMethod() {
        // any logic here
        // we can use $this->nonAbstractMethod() here too
    }

    // child class and overwrite the parent nonAbstractMethod too
}

PHP OOP Interfaces

Interface is a set of properties (variables) and methods (functions) that a class implementing that interface must have. Interface is different from abstract class (more below). A class can implement multiple interfaces. For example:

interface Animal {
    public $name;
    public function sleeps();
    public function eats();
}

class Cat implements Animal {
    public $name =  'tom';

    public function sleeps() {
        // any logic here
    }

    public function eats() {
        // any logic here
    }
}

Different between Interface & Abstract class

An abstract class can have abstract properties and methods as well as normal ones. However, interface is not a class, it’s just an interface, a set of rules or properties & methods that an implementing class must have.

A class can implement multiple interfaces, however it can be a child to only one abstract class.

PHP OOP Trait

A php class can only extend (or inherit from) a single class. We do have an option of interface, but interface just provides the declaration of methods, not the logic inside of them.

Traits are useful when we want to define a set of methods and use them in classes, besides their parent class code.

For example:

trait TraitName {
    public function hello() {
        echo "hello world!";
    }
}

class Child extends Parent implements SomeInterface {
    use TraitName;
}

Please let me know if you need help regarding php.

Leave a Reply

Your email address will not be published. Required fields are marked *

Hire Me!