繁体   English   中英

用类内部的函数初始化类变量

[英]Initialize class variable with function inside the 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

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

在类内部,您需要使用$this->来引用其他函数。

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

PS这是getstring ,而不是getsting

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM