从外部访问对象内的函数变量


Access a function variable within an object from outside

遵循以下结构:

class MyClass(){
   var prop;
   function myfunc(){
      $variable = 'value';
   }
}
$obj = new MyClass;

有没有办法从外面访问"$variable"而不返回?

我可以得到这样的属性:

$obj -> prop;

但是我无法访问这样的"$variable":

$obj -> variable;
class MyClass(){
   public prop;
   public variable;    
   function myfunc(){
      $this->variable = 'value';
   }
}

不建议在类中使用 var。 相反,您可以使用公共

$obj -> variable; now you can access this from out side the class scope

访问您的变量,例如

$obj -> variable;

你需要这样做:

class MyClass(){
   var prop;
   var variable;    
   function myfunc(){
      $this->variable = 'value';
   }
}