To pass value of a variable in one function to another function in same class

前端 未结 2 1798
情书的邮戳
情书的邮戳 2021-01-28 19:19

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

class Block_Model e         


        
相关标签:
2条回答
  • 2021-01-28 20:05

    You'll need private property:

    class Something{
       private $_variable = "";
    
       function input( $data ){
          $this->_variable = $data;
          //do the rest of function
       }
    
       function output(  ){
          //get previously set data
          echo $this->_variable;
       }
    
    }
    
    0 讨论(0)
  • 2021-01-28 20:07

    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;
    }
    

    Now you can use $value = $object->foo(); and $object->foo($value)->do_something_else();

    0 讨论(0)
提交回复
热议问题