- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章浅谈thinkphp5 instance 的简单实现由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
最近学习 ThinkPHP5,第一次看到 TestClass::instance() 就能创建 TestClass 实例的方法。感到很好奇,翻阅 ThinkPHP 的源代码,大体理解了 它的 设计思想,非常的先进.
老规矩,直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?php
class
TestClass {
public
static
function
instance() {
return
new
self();
}
public
$data
= [];
public
function
__set(
$name
,
$val
) {
return
$this
->data[
$name
] =
$val
;
}
public
function
__get(
$name
) {
return
$this
->data[
$name
];
}
}
$app1
= TestClass::instance();
$app1
->key =
'Application 1'
;
echo
$app1
->key .
'<br />'
;
?>
|
为了方便调用,也模仿 ThinkPHP 写了一个助手函数 。
1
2
3
4
5
6
7
8
9
|
<?php
function
app() {
return
TestClass::instance();
}
$app2
= app();
$app2
->key =
'Application 2'
;
echo
$app2
->key .
'<br />'
;
?>
|
这样就简单的实现了 instance.
不过这种方法还有一个小问题,试想以下,调用100次,就需要创建100个实例,想想都觉得可怕.
给 Test 类 增加一个 静态属性,将创建的实例保存到这里。下次如果需要调用,则直接调用这个实例.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<?php
class
TestClass {
public
static
$instance
;
//用于缓存实例
public
$data
= [];
public
static
function
instance() {
//如果不存在实例,则返回实例
if
(
empty
(self::
$instance
)) {
self::
$instance
=
new
self();
}
return
self::
$instance
;
}
public
function
__set(
$name
,
$val
) {
return
$this
->data[
$name
] =
$val
;
}
public
function
__get(
$name
) {
return
$this
->data[
$name
];
}
}
function
app(
$option
= []) {
return
TestClass::instance(
$option
);
}
header(
'content-type:text/plain'
);
$result
= [];
$app1
= app();
$app1
->key =
"Application 1"
;
//修改 key 为 Application 1
$result
[
'app1'
] = [
'app1'
=>
$app1
->key,
//实例中 key 为 Application 1
];
// 创建 app2,因为 instance 已经存在实例,直接返回 缓存的实例
$app2
= app();
$result
[
'app2'
] = [
'setp1'
=> [
'app1'
=>
$app1
->key,
// Application 1
'app2'
=>
$app2
->key,
//因为直接调用的实例的缓存,所以 key 也是 Application 1
],
];
// 无论 app1,app2 都对在内存中 对应的同一个实例,无论通过谁修改,都能改变值
$app1
->key =
"Application 2"
;
$result
[
'app2'
][
'setp2'
] = [
'app1'
=>
$app1
->key,
// Application 2
'app2'
=>
$app2
->key,
// Application 2
];
print_r(
$result
);
?>
|
通过上边的实验,可以看到 无论调用多少次,都会使用同一个实例。这样就解决了效率低的问题.
到现在基本就满足大多数情况了,唯一的小缺陷,就是 可能 实例的 初始参数不同,这样没法灵活调用(常见的比如同一个程序调用两个数据库)。在 上边的 例子中稍作改造,以传入的参数为key,将不通的 实例缓存到数组中 就可以解决.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<?php
class
TestClass {
public
static
$instance
= [];
//用于缓存实例数组
public
$data
= [];
public
function
__construct(
$opt
= []) {
$this
->data =
$opt
;
}
public
static
function
instance(
$option
= []) {
// 根据传入的参数 通过 serialize 转换为字符串,md5 后 作为数组的 key
$instance_id
= md5(serialize(
$option
));
//如果 不存在实例,则创建
if
(
empty
(self::
$instance
[
$instance_id
])) {
self::
$instance
[
$instance_id
] =
new
self(
$option
);
}
return
self::
$instance
[
$instance_id
];
}
public
function
__set(
$name
,
$val
) {
return
$this
->data[
$name
] =
$val
;
}
public
function
__get(
$name
) {
return
$this
->data[
$name
];
}
}
function
app(
$option
= []) {
return
TestClass::instance(
$option
);
}
header(
'content-type:text/plain'
);
$result
= [];
//传入 初始数据
$app1
= app([
'key'
=>
'123'
]);
$result
[
'init'
] =
$app1
->key;
// 使用 传入的数据,即:123
$app1
->key =
"app1"
;
$result
[
'app'
] =
$app1
->key;
// 现在值改为了 自定义的 app1了
print_r(
$result
);
$result
= [];
// 创建 app2,注意 初始参数不一样
$app2
= app();
// 因为初始参数不一样,所以还是创建新的实例
$app2
->key =
"app2"
;
$result
[
'app1'
] =
$app1
->key;
// app1
$result
[
'app2'
] =
$app2
->key;
// app2
print_r(
$result
);
$result
= [];
// 创建 app3,传入的参数 和 app1 一样,所以会直接返回 和app1相同 的 实例
$app3
= app([
'key'
=>
'123'
]);
$result
[
'log'
] = [
'app1'
=>
$app1
->key,
// app1
'app2'
=>
$app2
->key,
// app2
'app3'
=>
$app3
->key,
// app1
];
// 设置 app3 的key,会自动修改 app1 的值,因为他们两个是同一个实例
$app3
->key =
'app3'
;
$result
[
'app3_set'
] = [
'app1'
=>
$app1
->key,
// app3
'app2'
=>
$app2
->key,
// app2
'app3'
=>
$app3
->key,
// app3
];
// 同理,设置 app1 的key,app3 的 key 也会修改
$app1
->key =
'app1'
;
$result
[
'app1_set'
] = [
'app1'
=>
$app1
->key,
// app1
'app2'
=>
$app2
->key,
// app2
'app3'
=>
$app3
->key,
// app1
];
print_r(
$result
);
?>
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://www.miaoqiyuan.cn/p/php-class-instance?utm_source=tuicool&utm_medium=referral 。
最后此篇关于浅谈thinkphp5 instance 的简单实现的文章就讲到这里了,如果你想了解更多关于浅谈thinkphp5 instance 的简单实现的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在努力实现以下目标, 假设我有字符串: ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ) ) ) ) ) 我想编写一个正则
给定: 1 2 3 4 5 6
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
大家好,我卡颂。 Svelte问世很久了,一直想写一篇好懂的原理分析文章,拖了这么久终于写了。 本文会围绕一张流程图和两个Demo讲解,正确的食用方式是用电脑打开本文,跟着流程图、Demo一
身份证为15位或者18位,15位的全为数字,18位的前17位为数字,最后一位为数字或者大写字母”X“。 与之匹配的正则表达式: ?
我们先来最简单的,网页的登录窗口; 不过开始之前,大家先下载jquery的插件 本人习惯用了vs2008来做网页了,先添加一个空白页 这是最简单的的做法。。。先在body里面插入 <
1、MySQL自带的压力测试工具 Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、简单、实用的数据库文档(字典)生成工具,该工具支持CHM、Word、Excel、PDF、Html、XML、Markdown等
Go语言语法类似于C语言,因此熟悉C语言及其派生语言( C++、 C#、Objective-C 等)的人都会迅速熟悉这门语言。 C语言的有些语法会让代码可读性降低甚至发生歧义。Go语言在C语言的
我正在使用快速将 mkv 转换为 mp4 ffmpeg 命令 ffmpeg -i test.mkv -vcodec copy -acodec copy new.mp4 但不适用于任何 mkv 文件,当
我想计算我的工作簿中的工作表数量,然后从总数中减去特定的工作表。我错过了什么?这给了我一个对象错误: wsCount = ThisWorkbook.Sheets.Count - ThisWorkboo
我有一个 perl 文件,用于查看文件夹中是否存在 ini。如果是,它会从中读取,如果不是,它会根据我为它制作的模板创建一个。 我在 ini 部分使用 Config::Simple。 我的问题是,如果
尝试让一个 ViewController 通过标准 Cocoa 通知与另一个 ViewController 进行通信。 编写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDi
我正在绘制高程剖面图,显示沿路径的高程增益/损失,类似于下面的: Sample Elevation Profile with hand-placed labels http://img38.image
嗨,所以我需要做的是最终让 regStart 和 regPage 根据点击事件交替可见性,我不太担心编写 JavaScript 函数,但我根本无法让我的 regPage 首先隐藏。这是我的代码。请简单
我有一个非常简单的程序来测量一个函数花费了多少时间。 #include #include #include struct Foo { void addSample(uint64_t s)
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Simple.Data 的新手。但我很难找到如何进行“分组依据”。 我想要的是非常基本的。 表格看起来像: +________+ | cards | +________+ | id |
我现在正在开发一个 JS UDF,它看起来遵循编码。 通常情况下,由于循环计数为 2,Alert Msg 会出现两次。我想要的是即使循环计数为 3,Alert Msg 也只会出现一次。任何想法都
我是一名优秀的程序员,十分优秀!