Heim  >  Artikel  >  php教程  >  Review PHP设计模式之单例模式 - Jacky Yu

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

WBOY
WBOYOriginal
2016-05-20 13:15:171187Durchsuche

单例模式:

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

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn