将一个函数中变量的值传递给同一类中的另一个函数 [英] To pass value of a variable in one function to another function in same class

查看:63
本文介绍了将一个函数中变量的值传递给同一类中的另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类Block_Model(实际上是Kohana框架中的一个模型),带有2个方法input()output().

I have a class Block_Model (actually a model in Kohana framework) with 2 methods input()and output().

class Block_Model extends ORM {
    function input($arg) {
        //...
    }
    function output() {
        //...
    }
    //...
}

方法input是从称为Home_Controller的控制器内部编写的函数调用的,并将参数传递给方法input.

The method input is called from a function written inside a controller called Home_Controller and it passes an argument to the method input.

class Home_Controller extends Controller {
    function doSomething() {
        $block = new Block_Model();
        //...
        $block->input($val);
        //...
    }
}

如何在方法output()中访问传递给input()的参数?

How can I make the argument passed to input() be accessible in the method output()?

推荐答案

这类似于@silent的答案,但是您可以结合使用setter&一种方法中的吸气剂.

This is similar to @silent's answer, but you can combine setter & getter in one method.

protected $_foo;

public function foo($val = NULL)
{
    if ($val === NULL)
    {
        // its a getter!
        return $this->_foo;
    }

    // its a setter 
    $this->_foo = $val;
    // return current object, so it becomes a chainable method
    return $this;
}

现在您可以使用$value = $object->foo();$object->foo($value)->do_something_else();

这篇关于将一个函数中变量的值传递给同一类中的另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆