• yii2 container->set使用示例


    <?php
    class TestController extends Controller
    {
        public function actionIndex()
        {
            $order = [
                'id' => 1,
                'order_no' => 20201118
            ];
            $params = [
                'id' => 1,
                'name' => 'xiaoming'
            ];
            Yii::$container->set(TestPay::class, TestOrder::class, [$params, $order]);
            Yii::$container->invoke([$this, 'pay']);
            exit();
        }
    
        public function pay(TestPay $testPay)
        {
            $testPay->notify();
            $testPay->syncSms();
        }
    
    }
    
    abstract class TestPay
    {
        public $params;
        public $order;
    
        public function __construct($params, $order)
        {
            $this->params = $params;
            $this->order = $order;
        }
    
        abstract function validate();
    
        abstract function notify();
    
        public function syncSms()
        {
            print_r('syncSms');
            echo PHP_EOL;
        }
    }
    
    class TestOrder extends TestPay
    {
        public $params;
        public $order;
    
        public function __construct($params, $order)
        {
            parent::__construct($params, $order);
        }
    
        public function validate()
        {
            print_r('validate');
            echo PHP_EOL;
            print_r($this->params);
            echo PHP_EOL;
        }
    
        public function notify()
        {
            print_r('notify');
            echo PHP_EOL;
            print_r($this->order);
            echo PHP_EOL;
        }
    }

    效果截图【/test/index】:

  • 相关阅读:
    1月10日 TextView
    1月9日 布局2
    30 Adapter适配器
    29 个人通讯录列表(一)
    28 ListView控件
    27 登录模板
    26 Activity的启动模式
    25 Activity的生命周期
    24 得到Activity返回的数据
    23 Activity的传值2(bundle)
  • 原文地址:https://www.cnblogs.com/-mrl/p/14003829.html
Copyright © 2020-2023  润新知