Book Image

PhpStorm Cookbook

By : Mukund Chaudhary
Book Image

PhpStorm Cookbook

By: Mukund Chaudhary

Overview of this book

Table of Contents (16 chapters)
PhpStorm Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating delegate methods


Delegation is an important design pattern in software engineering. It enhances code reusability.

Getting ready

In PHP, delegate methods can be created in accordance with the principle of delegation. While calling delegate functions, you need not know the name of the actual function in advance—the PHP engine can find it out at runtime for you.

How to do it...

PHP provides two factory methods for the purpose. However, you can create your own delegate methods using the principles of object-oriented programming.

The factory methods provided by PHP are call_user_func() and call_user_func_array(). While the usage of the two methods is out of the scope of this text, the creation and usage of the delegate methods is better explained by the same scenario of cooking PizzaDish, as shown in the following code:

<?php
require_once 'Dish.php';

class PizzaDish{
  private $dishName;
  
  /**
    * @param mixed $dishName
  */
  public function setDishName($dishName)
  {
    $this...