我有这段代码:
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 A extends AA {
public $myobj;
public function __construct() {
$this->myobj = new MyObj();
}
public function getMyObj() {
return $this->myobj;
}
}
class B extends A {
public function __construct() {
parent::__construct();
$this->fun1();
}
private function fun1() {
$this->myobj->setVar1(TRUE);
}
}
class C extends A {
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();
问题是我需要一种方法来访问$this->myobj
OtherClass
var,以获取所有set()
函数的所有更新。
所以我期望在OtherClass
构造函数中得到以下结果:
var_dump ( $C->getMyObj()->getVar1() ); // true
var_dump ( $C->getMyObj()->getVar2() ); // true
var_dump ( $C->getMyObj()->getVar3() ); // 5
我不知道将MyObj
类与$C->getMyObj()
一起使用是否是跟踪各种类实例(B
,C
的更新的好方法, OtherClass
)。
如果不是,我接受任何提示。
由于
答案 0 :(得分:0)
刚刚使用singleton pattern
解决了问题。
问题是,从parent::__construct();
和B
调用C
确实会创建$this->myobj
($this->myobj = new MyObj();
)的新实例,所以我失去了我的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 A extends AA {
public function __construct() {
}
}
class B extends A {
public function __construct() {
parent::__construct();
$this->fun1();
}
private function fun1() {
$myobj = MyObj::getInstance();
$myobj->setVar1(TRUE);
}
}
class C extends A {
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();
编辑。
我编辑了这样的代码:
CREATE TABLE customer (
id INT AUTO_INCREMENT,
email VARCHAR(64) NOT NULL,
password VARCHAR(32) NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB;
CREATE TABLE movie (
id INT AUTO_INCREMENT,
title VARCHAR(64) NOT NULL,
runtime TIME NOT NULL,
certificate ENUM('U', 'PG', '12', '15', '18') NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB;
CREATE TABLE room (
id INT AUTO_INCREMENT,
roomNo ENUM ('Screen 1', 'Screen 2', 'Screen 3', 'Screen 4', 'Screen 5') NOT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB;
CREATE TABLE seat (
id INT AUTO_INCREMENT,
row ENUM('A', 'B', 'C', 'D', 'E', 'F') NOT NULL,
num ENUM('1', '2', '3', '4', '5', '6', '7', '8', '9', '10') NOT NULL,
roomID INT,
PRIMARY KEY (id),
FOREIGN KEY (roomID)
REFERENCES room (id)
) ENGINE = InnoDB;
CREATE TABLE screening (
id INT AUTO_INCREMENT,
movieID INT,
movieDate DATE NOT NULL,
movieTime TIME NOT NULL,
roomID INT,
PRIMARY KEY (id),
FOREIGN KEY (movieID)
REFERENCES movie (id),
FOREIGN KEY (roomID)
REFERENCES room (id)
) ENGINE = InnoDB;
CREATE TABLE booking (
id INT AUTO_INCREMENT,
customerID INT,
screeningID INT,
seatID INT,
PRIMARY KEY (id),
FOREIGN KEY (customerID)
REFERENCES customer(id),
FOREIGN KEY (screeningID)
REFERENCES screening (id),
FOREIGN KEY (seatID)
REFERENCES seat (id)
) ENGINE = InnoDB;