?php
class test{
public $x=1;
public $setting;
//負(fù)責(zé)生成其他對象的類或方法,這就是工廠模式
public function getSetting(){
if(!$this->setting){
$this->setting=new Setting();
}
return $this->setting;
}
}
class Setting{
public function __construct(){
echo 1111;
}
}
$test=new test();
$setting=$test->getSetting();
$setting2=$test->getSetting();
//判斷兩個對象是否是同一個對象
var_dump($setting===$setting2);
//看編號,也能看出來
var_dump($setting);
var_dump($setting2);
//屬性中有減號的處理
$name="x-b";
$test->$name=2;
var_dump($test);
//$test->x-b;//直接使用上面的屬性,會被認(rèn)為是一個減號
/*
報錯:
PHP Notice: Use of undefined constant b - assumed 'b' in D:\phpServer\WWW\test\
test.php on line 11
Notice: Use of undefined constant b - assumed 'b' in D:\phpServer\WWW\test\test.
php on line 11
*/
echo $test->{'x-b'}; //這種屬性里面有-的這樣包一下