Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to combine two variables to obtain / create new variable?

public $show_diary = 'my';

private my_diary(){
  return 1;
}

public view_diary(){
 return ${"this->"}.$this->show_diary.{"_diary()"}; // 1
 return $this->.{"$this->show_diary"}._diary() // 2
}

both return nothing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
180 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your class should be like following:

class Test
{
    public $show_diary;

    function __construct()
    {
        $this->show_diary = "my";
    }
    private function my_diary(){
      return 707;
    }

    public function view_diary(){
        echo $this->{$this->show_diary."_diary"}(); // 707
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...