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

妖精的绣舞 提交于 2019-12-02 11:09:41

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

}

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();

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!