<?<span style="color: #000000;">php </span><span style="color: #008080;">header</span>("content-type:text/html;charset=utf-8"<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">演示工厂模式</span><span style="color: #008000;"> /*</span><span style="color: #008000;"> 需求:请设计一个类,该类有一个方法,能够做到这种效果: 给该方法传入一个类名,则该方法就可以返回这个类的对象。就像一个 “塑料制品”工厂:给它一个某种产品的“模具”,就可以生产该模具对 应的塑料制品。 </span><span style="color: #008000;">*/</span> <span style="color: #008000;">//</span><span style="color: #008000;">工厂类</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Factory{ </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> GetObjectByClassName(<span style="color: #800080;">$class_name</span><span style="color: #000000;">){ </span><span style="color: #800080;">$obj1</span>=<span style="color: #0000ff;">new</span> <span style="color: #800080;">$class_name</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$obj1</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> A{}; </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> B{}; </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> C{}; </span><span style="color: #008000;">//</span><span style="color: #008000;">$F=new Factory(); //$obj1= $F->GetObjectByClassName("A"); <span style="color: #800080;">$obj1</span>= Factory::GetObjectByClassName("A"<span style="color: #000000;">); </span><span style="color: #800080;">$obj2</span>= Factory::GetObjectByClassName("A"<span style="color: #000000;">); </span><span style="color: #800080;">$obj3</span>= Factory::GetObjectByClassName("B"<span style="color: #000000;">); </span><span style="color: #800080;">$obj4</span>= Factory::GetObjectByClassName("C"<span style="color: #000000;">); </span><span style="color: #800080;">$obj5</span>= Factory::GetObjectByClassName("A"<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "<br />";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj1</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "<br />";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj2</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "<br />";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj3</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "<br />";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj4</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "<br />";<span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$obj5</span><span style="color: #000000;">); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> object(A)#2 (0) { } object(A)#3 (0) { } object(B)#4 (0) { } object(C)#5 (0) { } object(A)#6 (0) { } </span><span style="color: #008000;">*/</span>