首頁  >  文章  >  php教程  >  Review PHP设计模式之单例模式 - Jacky Yu

Review PHP设计模式之单例模式 - Jacky Yu

WBOY
WBOY原創
2016-05-20 13:15:171187瀏覽

单例模式:

<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();

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn