使用类中的函数初始化类变量

时间:2012-11-19 16:09:58

标签: php class

我有类似的课程:

class Example{
    private $a;
    private $b;

    function Example($user){
        $this->a = $user;
        $this->b = getsting();    //here is my problem
    }

    function getstring(){
        return "string".$this->a; //here I have a class variable
    }
}

如何将值返回$b

2 个答案:

答案 0 :(得分:0)

class Example
{
  private $a;
  private $b;

  function Example($user)
  {
    $this->userid=$user;
    $this->b=$this->getstring();    //use $this-> before the method name
  }

  function getstring()
  {
    return "string";
  }
}

答案 1 :(得分:0)

在课堂内,您需要使用$this->来引用其他功能。

$this->b = $this->getstring();

P.S。它是getstring,而不是getsting