WEB开发笔记 www.chhua.com 每日练习 PHP高级对象构建——使用创建方法

构建对象是PHP面向对象编程设计中的一个重要主题。在最简单的情况下,普通构造函数就够用了,但如果要开展更为复杂的设计,那么构造函数可能会变的难以管理。

下面就用一段代码示例来演示一下PHP高级对象构建中的使用创建方法进行对象构建的原理。

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
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> DateAndTime <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$timestamp</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$timestamp</span><span style="color: #339933">=</span><span style="color: #009900;font-weight: bold">FALSE</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><span style="color: #000088">$timestamp</span><span style="color: #009900">)</span> <span style="color: #000088">$timestamp</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/time" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">time</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//如果为空,刚是当前UNIX时间戳</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">timestamp</span><span style="color: #339933">=</span><span style="color: #000088">$timestamp</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//传入的参数赋值</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> createFromDateAndTime <span style="color: #009900">(</span>DateAndTime <span style="color: #000088">$datetime</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #000000;font-weight: bold">new</span> DateAndTime<span style="color: #009900">(</span><span style="color: #000088">$datetime</span><span style="color: #339933">-></span><span style="color: #004000">timestamp</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//相当于是克隆</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> creataFromString <span style="color: #009900">(</span><span style="color: #000088">$string</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//从时间字符串构建对象</span>
		<span style="color: #b1b100">return</span> <span style="color: #000000;font-weight: bold">new</span> DateAndTime<span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/strtotime" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">strtotime</span></a><span style="color: #009900">(</span><span style="color: #000088">$string</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//strtotime()函数是把时间字符串转换成 UNIX时间戳</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> getTimestamp<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//获取时间戳</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">timestamp</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$timeOne</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> DateAndTime<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//当前时间类</span>
<span style="color: #000088">$timeTwo</span><span style="color: #339933">=</span><span style="color: #000088">$timeOne</span><span style="color: #339933">-></span><span style="color: #004000">createFromDateAndTime</span><span style="color: #009900">(</span><span style="color: #000088">$timeOne</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//从timeOne对象复制一个对象到timeTwo</span>
<span style="color: #000088">$timeThree</span><span style="color: #339933">=</span><span style="color: #000088">$timeTwo</span><span style="color: #339933">-></span><span style="color: #004000">creataFromString</span><span style="color: #009900">(</span><span style="color: #0000ff">"2010-11-22"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//用timeTwo对象创建一个以字符串时间为参数的时间类timeThree</span>
<span style="color: #b1b100">echo</span> <span style="color: #000088">$timeThree</span><span style="color: #339933">-></span><span style="color: #004000">getTimestamp</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出时间戳</span>
<span style="color: #000000;font-weight: bold">?></span>

以上代码,就演示了一个简单的用创建方法构建对象的类,希望能给你带来帮助,关注我们www.chhua.com。
记住我的PHP进阶群号:63417682