OOP PHP - correct way to use a parent obj instance from children and other classes -
i have code:
class myobj { private $var1; private $var2; private $var3; function __construct() { $this->var1 = false; $this->var2 = false; $this->var3 = 0; } function getvar1() { return $this->var1; } function getvar2() { return $this->var2; } function getvar3() { return $this->var3; } function setvar1($var1) { $this->var1 = $var1; } function setvar2($var2) { $this->var2 = $var2; } function setvar3($var3) { $this->var3 = $var3; } } class aa { // ... } class extends aa { public $myobj; public function __construct() { $this->myobj = new myobj(); } public function getmyobj() { return $this->myobj; } } class b extends { public function __construct() { parent::__construct(); $this->fun1(); } private function fun1() { $this->myobj->setvar1(true); } } class c extends { public function __construct() { parent::__construct(); $this->fun2(); } private function fun2() { $this->myobj->setvar2(true); } } class otherclass extends aa { public function __construct() { $b = new b(); $c = new c(); $c->getmyobj()->setvar3(5); var_dump ( $c->getmyobj()->getvar1() ); // false var_dump ( $c->getmyobj()->getvar2() ); // true var_dump ( $c->getmyobj()->getvar3() ); // 5 echo "<br/><br/>"; var_dump ( $b->getmyobj()->getvar1() ); // null var_dump ( $b->getmyobj()->getvar2() ); // false var_dump ( $b->getmyobj()->getvar3() ); // 0 } } new otherclass();
the problem need way access $this->myobj
var otherclass
in order updates set()
functions. i'd expect following result in otherclass
constructor:
var_dump ( $c->getmyobj()->getvar1() ); // true var_dump ( $c->getmyobj()->getvar2() ); // true var_dump ( $c->getmyobj()->getvar3() ); // 5
i don't know either whether using myobj
class $c->getmyobj()
way track updates various class instances (b
, c
, otherclass
). if not accept tip.
thanks
just solved problem using singleton pattern
.
the problem that, calling parent::__construct();
b
, c
create new instance of $this->myobj
($this->myobj = new myobj();
) loosing edits.
i edited code that:
class myobj { private static $instance; private $var1; private $var2; private $var3; function __construct() { $this->var1 = false; $this->var2 = false; $this->var3 = 0; } function getvar1() { return $this->var1; } function getvar2() { return $this->var2; } function getvar3() { return $this->var3; } function setvar1($var1) { $this->var1 = $var1; } function setvar2($var2) { $this->var2 = $var2; } function setvar3($var3) { $this->var3 = $var3; } public static function getinstance(){ if( !isset( self::$instance ) ){ self::$instance = new myobj(); } return self::$instance; } } class aa { } class extends aa { public function __construct() { } } class b extends { public function __construct() { parent::__construct(); $this->fun1(); } private function fun1() { $myobj = myobj::getinstance(); $myobj->setvar1(true); } } class c extends { public function __construct() { parent::__construct(); $this->fun2(); } private function fun2() { $myobj = myobj::getinstance(); $myobj->setvar2(true); } } class otherclass extends aa { public function __construct() { $myobj = myobj::getinstance(); $b = new b(); $c = new c(); $myobj->setvar3(5); var_dump ( $myobj->getvar1() ); // true var_dump ( $myobj->getvar2() ); // true var_dump ( $myobj->getvar3() ); // 5 } } new otherclass();
Comments
Post a Comment