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
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
 
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> classUtil <span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//这是一个参数处理的类</span>
	<span style="color: #000000;font-weight: bold">public</span> static <span style="color: #000000;font-weight: bold">function</span> typeof<span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_object" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_object</span></a><span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/get_class" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">get_class</span></a><span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//如果是对象,获取类名</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_array</span></a><span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <span style="color: #0000ff">"array"</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//如果是数组,返回"array"</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_numeric" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_numeric</span></a><span style="color: #009900">(</span><span style="color: #000088">$var</span><span style="color: #009900">)</span><span style="color: #009900">)</span> <span style="color: #b1b100">return</span> <span style="color: #0000ff">"numeric"</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//如果是数字,返回"numeric"</span>
		<span style="color: #b1b100">return</span> <span style="color: #0000ff">"string"</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//字符串返回 "string"</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> static <span style="color: #000000;font-weight: bold">function</span> typelist<span style="color: #009900">(</span><span style="color: #000088">$args</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/array_map" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array_map</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"self"</span><span style="color: #339933">,</span><span style="color: #0000ff">"typeof"</span><span style="color: #009900">)</span><span style="color: #339933">,</span><span style="color: #000088">$args</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//数组循环通过调用self::typeof处理$args中的每个元素</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> static <span style="color: #000000;font-weight: bold">function</span> callMethodForArgs<span style="color: #009900">(</span><span style="color: #000088">$object</span><span style="color: #339933">,</span><span style="color: #000088">$args</span><span style="color: #339933">,</span><span style="color: #000088">$name</span><span style="color: #339933">=</span><span style="color: #0000ff">"construct"</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$method</span><span style="color: #339933">=</span><span style="color: #000088">$name</span><span style="color: #339933">.</span><span style="color: #0000ff">"_"</span><span style="color: #339933">.</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/implode" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">implode</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"_"</span><span style="color: #339933">,</span><span style="color: #000000;font-weight: bold">self</span><span style="color: #339933">::</span><span style="color: #004000">typelist</span><span style="color: #009900">(</span><span style="color: #000088">$args</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//implode 是把数组元素用"_"连接成一个字符串</span>
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_callable" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_callable</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #000088">$object</span><span style="color: #339933">,</span><span style="color: #000088">$method</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//is_callable()函数测试$object::$method是不是可调用的结构</span>
			<span style="color: #b1b100">echo</span> <a rel="noopener noreferrer nofollow" href="http://www.php.net/sprintf" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">sprintf</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"Class <span style="color: #009933;font-weight: bold">%s</span> has no methd '<span style="color: #006699;font-weight: bold">$name</span>' that takes"</span><span style="color: #339933">.</span>
			<span style="color: #0000ff">"arguments (<span style="color: #009933;font-weight: bold">%s</span>)"</span><span style="color: #339933">,</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/get_class" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">get_class</span></a><span style="color: #009900">(</span><span style="color: #000088">$object</span><span style="color: #009900">)</span><span style="color: #339933">,</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/implode" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">implode</span></a><span style="color: #009900">(</span><span style="color: #0000ff">","</span><span style="color: #339933">,</span><span style="color: #000000;font-weight: bold">self</span><span style="color: #339933">::</span><span style="color: #004000">typelist</span><span style="color: #009900">(</span><span style="color: #000088">$args</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<a rel="noopener noreferrer nofollow" href="http://www.php.net/call_user_func_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">call_user_func_array</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span><span style="color: #000088">$object</span><span style="color: #339933">,</span><span style="color: #000088">$method</span><span style="color: #009900">)</span><span style="color: #339933">,</span><span style="color: #000088">$args</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//call_user_func_array函数调用$object::$method($args)</span>
		<span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</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">$timetamp</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: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//自身的构造函数</span>
		<span style="color: #000088">$args</span><span style="color: #339933">=</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/func_get_args" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">func_get_args</span></a><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//获取参数</span>
		classUtil<span style="color: #339933">::</span><span style="color: #004000">callMethodForArgs</span><span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">,</span><span style="color: #000088">$args</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> construct_<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: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">timetamp</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: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> construct_dateAndTime<span style="color: #009900">(</span><span style="color: #000088">$datetime</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//为类自身的时候</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">timetamp</span><span style="color: #339933">=</span><span style="color: #000088">$datetime</span><span style="color: #339933">-></span><span style="color: #004000">getTimetamp</span><span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> construct_number<span style="color: #009900">(</span><span style="color: #000088">$timestamp</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//为数字的时候</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">timetamp</span><span style="color: #339933">=</span><span style="color: #000088">$timestamp</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> construct_string<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: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">timetamp</span><span style="color: #339933">=</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: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> getTimetamp<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">timetamp</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #000000;font-weight: bold">?></span>

以上方法,就说明了多个构造函数的使用方法,其实,很简单,主要是对参数进行了处理,不管是参数是字符,还是数字,还是类,都先进了不同的处理,这样就加大了代码的灵活性。关注面向对象,关注PHP,请 记住我的PHP进阶群(63417682)和我的网站www.chhua.com,