简体   繁体   中英

Access a function variable within an object from outside

Following this structure:

class MyClass(){
   var prop;

   function myfunc(){
      $variable = 'value';
   }
}

$obj = new MyClass;

Is there a way i can access '$variable' from outside without a return?

I can get a property like this:

$obj -> prop;

but i can't access the '$variable' like this:

$obj -> variable;
class MyClass(){
   public prop;
   public variable;    

   function myfunc(){
      $this->variable = 'value';
   }
}

using var inside the class is not recommended. instead of that you can use public

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

To access your variable like

$obj -> variable;

You need to make like this:

class MyClass(){
   var prop;
   var variable;    

   function myfunc(){
      $this->variable = 'value';
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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