温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP工厂模式的日常使用方法介绍

发布时间:2020-04-15 10:36:02 来源:亿速云 阅读:165 作者:小新 栏目:编程语言

这篇文章主要介绍了PHP工厂模式的日常使用方法,具有一定借鉴价值,需要的朋友可以参考下。下面就和我一起来看看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'}; //这种属性里面有-的这样包一下

PHP工厂模式的日常使用方法介绍

以上就是PHP工厂模式的日常使用方法介绍的详细内容了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎来亿速云行业资讯!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI