Chaining Static Methods in PHP?

前端 未结 16 2125
情歌与酒
情歌与酒 2020-11-27 14:25

Is it possible to chain static methods together using a static class? Say I wanted to do something like this:

$value = TestClass::toValue(5)::add(3)::subtrac         


        
16条回答
  •  青春惊慌失措
    2020-11-27 14:43

    The most easiest way i have ever found for method chaining from new Instance or Static method of class is as below. I have used Late Static Binding here and i really loved this solution.

    I have created a utility to send multiple User Notification on next page using tostr in Laravel.

     false,
    
            "debug" => false,
    
            "newestOnTop" => false,
    
            "progressBar" => false,
    
            "positionClass" => "toast-top-right",
    
            "preventDuplicates" => false,
    
            "onclick" => null,
    
            "showDuration" => "3000",
    
            "hideDuration" => "1000",
    
            "timeOut" => "5000",
    
            "extendedTimeOut" => "1000",
    
            "showEasing" => "swing",
    
            "hideEasing" => "linear",
    
            "showMethod" => "fadeIn",
    
            "hideMethod" => "fadeOut"
        ];
    
        private static $toastType = "success";
    
        private static $instance;
    
        private static $title;
    
        private static $message;
    
        private static $toastTypes = ["success", "info", "warning", "error"];
    
        public function __construct($options = [])
        {
            self::$options = array_merge(self::$options, $options);
        }
    
        public static function setOptions(array $options = [])
        {
            self::$options = array_merge(self::$options, $options);
    
            return self::getInstance();
        }
    
        public static function setOption($option, $value)
        {
            self::$options[$option] = $value;
    
            return self::getInstance();
        }
    
        private static function getInstance()
        {
            if(empty(self::$instance) || self::$instance === null)
            {
                self::setInstance();
            }
    
            return self::$instance;
        }
    
        private static function setInstance()
        {
            self::$instance = new static();
        }
    
        public static function __callStatic($method, $args)
        {
            if(in_array($method, self::$toastTypes))
            {
                self::$toastType = $method;
    
                return self::getInstance()->initToast($method, $args);
            }
    
            throw new \Exception("Ohh my god. That toast doesn't exists.");
        }
    
        public function __call($method, $args)
        {
            return self::__callStatic($method, $args);
        }
    
        private function initToast($method, $params=[])
        {
            if(count($params)==2)
            {
                self::$title = $params[0];
    
                self::$message = $params[1];
            }
            elseif(count($params)==1)
            {
                self::$title = ucfirst($method);
    
                self::$message = $params[0];
            }
    
            $toasters = [];
    
            if(Session::has('toasters'))
            {
                $toasters = Session::get('toasters');
            }
    
            $toast = [
    
                "options" => self::$options,
    
                "type" => self::$toastType,
    
                "title" => self::$title,
    
                "message" => self::$message
            ];
    
            $toasters[] = $toast;
    
            Session::forget('toasters');
    
            Session::put('toasters', $toasters);
    
            return $this;
        }
    
        public static function renderToasters()
        {
            $toasters = Session::get('toasters');
    
            $string = '';
    
            if(!empty($toasters))
            {
                $string .= '';
            }
    
            Session::forget('toasters');
    
            return new HtmlString($string);
        }
    }
    

    This will work as below.

    Toaster::success("Success Message", "Success Title")
    
        ->setOption('showDuration', 5000)
    
        ->warning("Warning Message", "Warning Title")
    
        ->error("Error Message");
    

提交回复
热议问题