Delegation is an important design pattern in software engineering. It enhances code reusability.
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.
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...