Daily Use of PHP Factory Mode
- 2021-12-04 09:38:55
- OfStack
The class or method responsible for generating other objects, this is the factory pattern, and the following is a common use
<?php
class test{
public $x=1;
public $setting;
// Class or method responsible for generating other objects , This is the factory model
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();
// Determine whether two objects are the same 1 Objects
var_dump($setting===$setting2);
// Look at the number , You can also see that
var_dump($setting);
var_dump($setting2);
// Handling with minus sign in attribute
$name="x-b";
$test->$name=2;
var_dump($test);
//$test->x-b;// Use the above properties directly , Will be considered as 1 A minus sign
/*
Report an error :
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'}; // This attribute contains - Such a package of 1 Under