Petru Lebada
Petru Lebada

Reputation: 1682

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;

Upvotes: 1

Views: 76

Answers (2)

Y. M.
Y. M.

Reputation: 107

To access your variable like

$obj -> variable;

You need to make like this:

class MyClass(){
   var prop;
   var variable;    

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

Upvotes: 1

Subash
Subash

Reputation: 230

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

Upvotes: 2

Related Questions