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();
posted @ 2019-07-19 09:45  三百里江山  阅读(633)  评论(0编辑  收藏  举报