PHP: Variable scope in OOP?

In your code, $myVar is local to each method. Perhaps you meant $this->myVar?

1 Yup: $this-> when in an object context, self:: when in a static context. – Pekka Jun 29 '10 at 16:52.

You need to use the $this 'pointer'. E.g. : class Test { protected $var; public function __construct() { $this->var = 'foobar'; } public function getVar() { return $this->var; } }.

Class Manual extends controller { private $myVar; function Manual(){ parent::Controller(); $this->$myVar = 'blablabla'; } function doStuff(){ echo $this->$myVar; } } Even more OOP-like with Setters/Getters class Manual extends controller { private $myVar; function Manual(){ parent::Controller(); setMyVar('blablabla'); } function doStuff(){ echo getMyVar(); } function getMyVar() { return $this->myVar; } function setMyVar($var) { $this->myVar = $var; }.

The variable $myVar should be property of a class, and you can not do: echo $myVar; You should do: $this->myVar.

As written, $myVar is local to both methods. You need to declare $myVar as a property in the class body protected $myVar; and then use the pseudo variable $this to access the property in methods, including the constructor $this->myVar.

$myVar field must be declarated as public/protected in the parent class or declarated in the descedent class, and in yours doStuff() method you must write $this->myVar not the $myVar.

So it seems the scope of the variable is not right. Does anyone know how to fix this? It would be best if MyClass would not need an extra reference or something to mysqli, since I would like to keep it seperated.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions