<?php namespace StockExample; interface Order { public function execute(); } class Stock { static public function buy(): callable { return function () { echo "You want to buy stocks via callable function" . PHP_EOL; }; // or as callable object // return new StockBuy(); } static public function sell(): callable { return function () { echo "You want to sell stocks via callable function" . PHP_EOL; }; // or as callable object // return new StockSell(); } } class MyCommand implements Order { private $action; public function __construct(callable $action) { $this->action = $action; } public function execute() { // Option 1) use call_user_function call_user_func($this->action); // Option 2) define it as a variable and call it by adding `()` //$action = $this->action; //$action(); } } $bsc = new MyCommand(Stock::buy()); $ssc = new MyCommand(Stock::sell()); $bsc->execute(); $ssc->execute();
You have javascript disabled. You will not be able to edit any code.