PHP工厂模式的日常使用
负责生成其他对象的类或方法,这就是工厂模式,下面是一个经常见到的用法
<?php class test{ public $x=1; public $setting; //负责生成其他对象的类或方法,这就是工厂模式 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;//直接使用上面的属性,会被认为是一个减号 /* 报错: 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'}; //这种属性里面有-的这样包一下
相关推荐
wwwsurfphpseocom 2020-10-28
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23