首頁 >後端開發 >php教程 >php物件導向要點[轉]

php物件導向要點[轉]

WBOY
WBOY原創
2016-07-30 13:31:21814瀏覽

  • __construct
<code><span><span><?php</span><span><span>class</span><span>ConstructTest</span> {</span><span>private</span><span>$arg1</span>;
    <span>private</span><span>$arg2</span>;

    <span>public</span><span><span>function</span><span>__construct</span><span>(<span>$arg1</span>, <span>$arg2</span>)</span> {</span><span>$this</span>->arg1 = <span>$arg1</span>;
        <span>$this</span>->arg2 = <span>$arg2</span>;
        <span>print</span><span>"__construct is called...\n"</span>;
    }
    <span>public</span><span><span>function</span><span>printAttributes</span><span>()</span> {</span><span>print</span><span>'$arg1 = '</span>.<span>$this</span>->arg1.<span>' $arg2 = '</span>.<span>$this</span>->arg2.<span>"\n"</span>;
    }
}
<span>$testObject</span> = <span>new</span> ConstructTest(<span>"arg1"</span>,<span>"arg2"</span>); 
<span>$testObject</span>->printAttributes();</span></span></code>

運行結果如下:

<code><span>Stephens</span>-<span>Air</span><span>:Desktop</span><span>$ </span>php <span>Test</span>.php 
__construct is called...
<span>$arg1</span> = arg1 <span>$arg2</span> = arg2</code>
  • parent

用於在子類中直接調用父類

在類別內呼叫該類別靜態成員和靜態方法的前綴修飾,對於非靜態成員變數和函數則使用this。

<code><span><span><?php</span><span><span>class</span><span>BaseClass</span> {</span><span>protected</span><span>$arg1</span>;
    <span>protected</span><span>$arg2</span>;

    <span><span>function</span><span>__construct</span><span>(<span>$arg1</span>, <span>$arg2</span>)</span> {</span><span>$this</span>->arg1 = <span>$arg1</span>;
        <span>$this</span>->arg2 = <span>$arg2</span>;
        <span>print</span><span>"__construct is called...\n"</span>;
    }
    <span><span>function</span><span>getAttributes</span><span>()</span> {</span><span>return</span><span>'$arg1 = '</span>.<span>$this</span>->arg1.<span>' $arg2 = '</span>.<span>$this</span>->arg2;
    }
}

<span><span>class</span><span>SubClass</span><span>extends</span><span>BaseClass</span> {</span><span>protected</span><span>$arg3</span>;

    <span><span>function</span><span>__construct</span><span>(<span>$baseArg1</span>, <span>$baseArg2</span>, <span>$subArg3</span>)</span> {</span><span>parent</span>::__construct(<span>$baseArg1</span>, <span>$baseArg2</span>);
        <span>$this</span>->arg3 = <span>$subArg3</span>;
    }
    <span><span>function</span><span>getAttributes</span><span>()</span> {</span><span>return</span><span>parent</span>::getAttributes().<span>' $arg3 = '</span>.<span>$this</span>->arg3;
    }
}
<span>$testObject</span> = <span>new</span> SubClass(<span>"arg1"</span>,<span>"arg2"</span>,<span>"arg3"</span>); 
<span>print</span><span>$testObject</span>->getAttributes().<span>"\n"</span>;</span></span></code>
  • static
    這裡介紹的static關鍵字主要用於PHP 5.3以上版本新增的延遲靜態綁定功能。
<code><span>Stephens</span>-<span>Air</span><span>:Desktop</span><span>$ </span>php <span>Test</span>.php 
__construct is called...
<span>$arg1</span> = arg1 <span>$arg2</span> = arg2 <span>$arg3</span> = arg3</code>
    運行結果如下:
  • <code><span><span><?php</span><span><span>class</span><span>StaticExample</span> {</span><span>static</span><span>public</span><span>$arg1</span> = <span>"Hello, This is static field.\n"</span>;
        <span>static</span><span>public</span><span><span>function</span><span>sayHello</span><span>()</span> {</span><span>print</span><span>self</span>::<span>$arg1</span>;
        }
    }
    
    <span>print</span> StaticExample::<span>$arg1</span>;
    StaticExample::sayHello();</span></code>

    static關鍵字不僅可以用於實例化。和self和parent一樣,static也可以作為靜態方法呼叫的標識符,甚至是從非靜態上下文中呼叫。在該場景下,self仍然表示的是目前方法所在的類別。請參閱下列程式碼:
  • <code><span><span><?php</span><span>abstract</span><span><span>class</span><span>Base</span> {</span><span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span> {</span><span>//这里的new static()实例化的是调用该静态方法的当前类。</span><span>return</span><span>new</span><span>static</span>();
        }
        <span>abstract</span><span>public</span><span><span>function</span><span>printSelf</span><span>()</span>;</span>}
    
    <span><span>class</span><span>SubA</span><span>extends</span><span>Base</span> {</span><span>public</span><span><span>function</span><span>printSelf</span><span>()</span> {</span><span>print</span><span>"This is SubA::printSelf.\n"</span>;
        }
    }
    
    <span><span>class</span><span>SubB</span><span>extends</span><span>Base</span> {</span><span>public</span><span><span>function</span><span>printSelf</span><span>()</span> {</span><span>print</span><span>"This is SubB::printSelf.\n"</span>;
        }
    }
    
    SubA::getInstance()->printSelf();
    SubB::getInstance()->printSelf();</span></span></code>
運作結果如下:

<code>Stephens-Air:Desktop$ php Test.php 
This <span>is</span> SubA<span>::printSelf.</span>
This <span>is</span> SubB<span>::printSelf.</span></code>

__destruct

析構方法的作用和建構方法__construct剛好相反,它只是在物件被垃圾收集器收集之前自動調用,我們可以利用該方法做一些必要的清理工作。

<code><span><span><?php</span><span>abstract</span><span><span>class</span><span>Base</span> {</span><span>private</span><span>$ownedGroup</span>;
    <span>public</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>//这里的static和上面的例子一样,表示当前调用该方法的实际类。</span><span>//需要另外说明的是,这里的getGroup方法即便不是静态方法,也会得到相同的结果。然而倘若</span><span>//getGroup真的只是普通类方法,那么这里还是建议使用$this。</span><span>$this</span>->ownedGroup = <span>static</span>::getGroup();
    }
    <span>public</span><span><span>function</span><span>printGroup</span><span>()</span> {</span><span>print</span><span>"My Group is "</span>.<span>$this</span>->ownedGroup.<span>"\n"</span>;
    }
    <span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span> {</span><span>return</span><span>new</span><span>static</span>();
    }
    <span>public</span><span>static</span><span><span>function</span><span>getGroup</span><span>()</span> {</span><span>return</span><span>"default"</span>;
    }
}

<span><span>class</span><span>SubA</span><span>extends</span><span>Base</span> {</span>
}

<span><span>class</span><span>SubB</span><span>extends</span><span>Base</span> {</span><span>public</span><span>static</span><span><span>function</span><span>getGroup</span><span>()</span> {</span><span>return</span><span>"SubB"</span>;
    }
}

SubA::getInstance()->printGroup();
SubB::getInstance()->printGroup(); </span></span></code>
    運作結果如下:
  • <code>
    Stephens-Air:Desktop$ php Test.php 
    My <span>Group</span><span>is</span><span>default</span>
    My <span>Group</span><span>is</span> SubB</code>

  • __clone
在PHP 5之後的版本中,物件之間的賦值為引用賦值,即賦值後的兩個物件將指向同一位址空間,如果想基於物件賦值,可以使用PHP提供的clone方法。此方法將目前物件淺拷貝之後的副本傳回,如果想在clone的過程中完成一些特殊的操作,如深拷貝,則需要在目前類別的宣告中實作__clone方法,該方法在執行clone的過程中會被隱式調用。另外要格外注意的是,__clone方法是作用再被拷貝的物件上,也就是賦值後的物件上執行。

<code><span><span><?php</span><span><span>class</span><span>TestClass</span> {</span><span><span>function</span><span>__destruct</span><span>()</span> {</span><span>print</span><span>"TestClass destructor is called.\n"</span>;
    }
}

<span>$testObj</span> = <span>new</span> TestClass();
<span>unset</span>(<span>$testObj</span>);
<span>print</span><span>"Application will exit.\n"</span>;</span></code>
    運作結果如下:
  • <code>
    Stephens<span>-Air</span>:Desktop$ php Test<span>.</span>php 
    Before calling <span>to</span> clone<span>.</span>
    __clone is called<span>.</span>
    After calling <span>to</span> clone<span>.</span><span>In</span> outerA: <span>$id</span><span>=</span><span>20</span><span>In</span> outerB: <span>$id</span><span>=</span><span>10</span></code>
    • const
      PHP5可以在类中定义常量属性。和全局常量一样,一旦定义就不能改变。常量属性不需要像普通属性那样以访

      以上就介绍了php面向对象要点[转],包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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