Access class property from outside of class

前端 未结 6 442
感情败类
感情败类 2021-01-22 18:41

Suppose I had the following class:

class MyClass {
    public function Talk() {
        $Say = \"Something\";
        return $Say;
    }
}

I th

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-22 19:18

    $say is not a class property. If it was, you would define your class like this:

    class MyClass {
        public $say;      
    }
    

    It is instead a local variable of the function Talk(). If you want to access it the way that you have the class defined, you would do:

    $instance = new MyClass();
    $instance->Talk();
    

提交回复
热议问题