How to use the power of inheritance with composition in php
php.net

How to use the power of inheritance with composition in php

Inheritance and composition are two most basic and powerful components of OOP. They both have pros and cons but composition offers more flexibility as it lets you change the behavior being composed at run-time. Anyway, I am not going to the detail discussion in this article.

Imagine the scenario, you want to use composition but treat it like inheritance e.g. call composed object's method in the same way as calling the methods of the parent class. Here is how you can do it PHP by using __call and __callStatic magic method.

public class Wheel{

  public function turnLeft($howMuch){
    //some code here
  }

}?

public function Car{
  $protect wheel;

  public function __construct(Wheel $wheel)
  {
    $this->wheel = $wheel;
  }
}?

Now let's suppose you want to call turnLeft method of Wheel with Car class.

$wheel = new Wheel;

car = new Car($wheel);

car->turnLeft(5);

We can do this by using PHP magic methods __call and __callStatic. Here is how.

Define __call magic method in Car class

public function __call($method, $parameters)
{
  return call_user_func([$this->wheel, $method], $parameters);
}?

When you will call turnLeft method on you car class, as this method doesn't exist in this class, the __call magic method will be called. The __call magic method will call the required method of the Wheel class.

For static methods, we can have the same approach, we just have to implement the __callStatic magic method.

public function __callStatic($method, $parameters)
{
  return call_user_func([$this->wheel, $method], $parameters);
}?

This is the most basic example but it can be handy in real world scenarios.

Shan Usman

PHP Developer(Laravel framework) and Frontend designer at Internetesolutions.com

7 年

i want to internship in php please call me.

回复
Shams hashmi

Technical Team Lead specializing in Back End and Front End Development

8 年

Great article.

要查看或添加评论,请登录

Taukeer Liaqat的更多文章

  • Avoid bloating routes file with partial namespaces

    Avoid bloating routes file with partial namespaces

    In the latest version, Laravel has removed automatic namespace prefixing for the controllers. Now you can use class…

    1 条评论
  • How FormRequest validation works in Laravel(PHP)

    How FormRequest validation works in Laravel(PHP)

    Form Request validation is very simple and elegant way to validate input data coming from the client side. As you may…

    7 条评论
  • How Global Scope works in Laravel(PHP)

    How Global Scope works in Laravel(PHP)

    What is global scope I am just copying original description of global scope from the Laravel's official site. Which is…

    1 条评论
  • How SoftDelete works in php framework laravel 5.3/5.4

    How SoftDelete works in php framework laravel 5.3/5.4

    Laravel’s SoftDelete is the combination of two things A global scope to limit soft deleted records. An overridden…

    3 条评论

社区洞察

其他会员也浏览了