单例模式:
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Single { </span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$_instance</span><span style="color: #000000;">; </span><span style="color: #008080;"> 3</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct(){ </span><span style="color: #008080;"> 5</span> <span style="color: #008000;">//</span><span style="color: #008000;">define method as private</span> <span style="color: #008080;"> 6</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __clone(){ </span><span style="color: #008080;"> 9</span> <span style="color: #008080;">trigger_error</span>('Clone is not allow!', <span style="color: #ff00ff;">E_USER_ERROR</span><span style="color: #000000;">); </span><span style="color: #008080;">10</span> <span style="color: #000000;"> } </span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getInstance(){ </span><span style="color: #008080;">13</span> <span style="color: #0000ff;">if</span>(!(self::<span style="color: #800080;">$_instance</span><span style="color: #000000;"> instanceof self)){ </span><span style="color: #008080;">14</span> self::<span style="color: #800080;">$_instance</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> self; </span><span style="color: #008080;">15</span> <span style="color: #000000;"> } </span><span style="color: #008080;">16</span> <span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$_instance</span><span style="color: #000000;">; </span><span style="color: #008080;">17</span> <span style="color: #000000;"> } </span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> show(){ </span><span style="color: #008080;">20</span> <span style="color: #008080;">phpinfo</span><span style="color: #000000;">(); </span><span style="color: #008080;">21</span> <span style="color: #000000;"> } </span><span style="color: #008080;">22</span> <span style="color: #000000;">} </span><span style="color: #008080;">23</span> <span style="color: #800080;">$single</span> = Single::<span style="color: #000000;">getInstance(); </span><span style="color: #008080;">24</span> <span style="color: #800080;">$single</span>->show();