zoukankan      html  css  js  c++  java
  • php静态调用非静态方法

    <?php
    /**
     * php静态调用非静态方法
     * author: 百里
     * Date: 2019/7/18
     * Time: 17:28
     */
    
    function dump(...$var) {
        foreach ($var as $v) {
            var_dump($v);
        }
    }
    
    class Model
    {
        private $where = [];
        private $update = [];
        private static $self = null;
    
        /**
         * @return null|Projects
         */
        private static function getInstance() {
            return new self();
        }
    
        public function where($field, $value) {
            $obj = self::getInstance();
            $obj->where[$field] = $value;
            return $obj;
        }
        public function update(array $params) {
            $obj = self::getInstance();
            $obj->update = $params;
        }
        public function get() {
            dump(self::getInstance()->where);
        }
    
        /**
         * 实现静态调用非静态方法
         * @param $method
         * @param $arguments
         * @return mixed
         * @throws Exception
         */
        public static function __callStatic($method, $arguments)
        {
            $obj = self::getInstance();
            if (method_exists($obj, $method)) {
                return call_user_func_array([$obj, $method], $arguments);
            }
            throw new Exception('static is call failed');
        }
    }
    
    Model::where('ab', 100)->update([100]);
    Model::where('ab', 200)->update([200]);
    Model::get();
    Model::get();
    Model::get();
    
  • 相关阅读:
    iou与giou对比
    Linux学习第一天 vim
    奖励加分申请
    人月神话阅读笔记3
    5.27
    5.26
    5.25
    5.23
    5.22
    5.21
  • 原文地址:https://www.cnblogs.com/300js/p/11211418.html
Copyright © 2011-2022 走看看