【问题标题】:calling multiple level function in class在类中调用多级函数
【发布时间】:2014-01-02 01:32:00
【问题描述】:

我看到一些免费的类库做了一些非常有趣的事情,比如

wideimage,phpexcel 做的,它调用如下

$class= 新宽幅图像;

//宽幅图像

$class->load()->resize()->addnoise()->saveToFile();

我想学习它,但我不知道,我认为它使用函数 __call(){} 但我试过了,它不起作用,有人可以教我怎么做吗?我尝试搜索 stackoverflow 和谷歌,但我不知道我应该搜索什么关键字 LOL。

<?php

namespace system\classes {

    if(!defined('IN_APP')){

        exit('ACCESS DENIED.');

    }

    class images
    {

        protected $_root;
        protected $_attachment;
        protected $_image_path;
        protected $image;
        public $image_container;

        public function __construct(){

            global $_G;

            $this->_root = getglobal('attachment/path/root');
            $this->_attachment = getglobal('attachment/path/sub');
            $this->_image_path = $this->_root . DS . $this->_attachment . DS . getglobal('attachment/path/images') . DS;

            if(!$this->image || $this->image == '' || null($this->image)){

                $this->image = new \system\classes\wideimage\WideImage();

            }

        }

        public function load($var){
            $matrix = array(array(2,0,0),array(0, -1, 0), array(0, 0, -1));
            // $this->image = $this->image->loadFromUpload($var)->crop(50,50)->resize(100,100)->rotate(200,200)->addNoise(300, 'mono')->applyConvolution($matrix, 1, 220);
            $this->image = $this->image->loadFromUpload($var);

            // exit(var_dump($this->_image_path));

        }

        public function resize($width, $height){

            $this->image = $this->image->resize($width, $height);

        }

        public function save(){
            // exit(var_dump($this->image_container));
            $this->image = $this->image->saveToFile('name.gif');

        }

        public function __call($name,$arg){

            exit(var_dump($name));

        }

    }

}

?>

这是我的 test.php

if(isset($_FILES['img'])){

        $images = new system\classes\images();

        $images->load('img')->save();

        // $images->save();

    }

我尝试了 $images->load('img')->save();

和php给我一个错误

致命错误:在非对象中调用成员函数 save()

【问题讨论】:

    标签: php function class


    【解决方案1】:

    -&gt; 运算符调用对象的方法。因此你必须从你的函数中返回一个对象,所以你有一些东西可以调用你的方法。最简单的方法是只使用return $this,这样您就可以再次拥有与初始方法调用相同的对象实例。

    关键字是method chaining

    【讨论】:

    • 这么简单,但我不知道要搜索什么关键字...谢谢
    猜你喜欢
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    相关资源
    最近更新 更多