[PHP] A common use method of factory pattern

little bottle
Release: 2023-04-05 22:10:02
forward
2098 people have browsed it

Responsible for generating classes or methods of other objects. This is the factory pattern. The following is a commonly seen usage.


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'}; //这种属性里面有-的这样包一下
Copy after login

Related tutorials: PHP video tutorial

The above is the detailed content of [PHP] A common use method of factory pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!