language.oop5.basic.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 195 行 · 第 1/2 页
HTML
195 行
</div> <div id="language.oop5.basic.new" class="sect2"> <h3 class="title">new</h3> <p class="para"> To create an instance of a class, a new object must be created and assigned to a variable. An object will always be assigned when creating a new object unless the object has a <a href="language.oop5.decon.html" class="link">constructor</a> defined that throws an <a href="language.exceptions.html" class="link">exception</a> on error. Classes should be defined before instantiation (and in some cases this is a requirement). </p> <div class="example"> <p><b>Example #5 Creating an instance</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$instance </span><span style="color: #007700">= new </span><span style="color: #0000BB">SimpleClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> In the class context, it is possible to create a new object by <i>new self</i> and <i>new parent</i>. </p> <p class="para"> When assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned. This behaviour is the same when passing instances to a function. A copy of an already created object can be made by <a href="language.oop5.cloning.html" class="link">cloning</a> it. </p> <div class="example"> <p><b>Example #6 Object Assignment</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$assigned </span><span style="color: #007700">= </span><span style="color: #0000BB">$instance</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$reference </span><span style="color: #007700">=& </span><span style="color: #0000BB">$instance</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$instance</span><span style="color: #007700">-></span><span style="color: #0000BB">var </span><span style="color: #007700">= </span><span style="color: #DD0000">'$assigned will have this value'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$instance </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">; </span><span style="color: #FF8000">// $instance and $reference become null<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$instance</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$reference</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$assigned</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>NULLNULLobject(SimpleClass)#1 (1) { ["var"]=> string(30) "$assigned will have this value"}</pre></div> </pre></div> </div> </div> <div id="language.oop5.basic.extends" class="sect2"> <h3 class="title">extends</h3> <p class="para"> A class can inherit methods and members of another class by using the extends keyword in the declaration. It is not possible to extend multiple classes, a class can only inherit one base class. </p> <p class="para"> The inherited methods and members can be overridden, unless the parent class has defined a method as <a href="language.oop5.final.html" class="link">final</a>, by redeclaring them with the same name defined in the parent class. It is possible to access the overridden methods or static members by referencing them with <a href="language.oop5.paamayim-nekudotayim.html" class="link">parent::</a> </p> <div class="example"> <p><b>Example #7 Simple Class Inheritance</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">ExtendClass </span><span style="color: #007700">extends </span><span style="color: #0000BB">SimpleClass<br /></span><span style="color: #007700">{<br /> </span><span style="color: #FF8000">// Redefine the parent method<br /> </span><span style="color: #007700">function </span><span style="color: #0000BB">displayVar</span><span style="color: #007700">()<br /> {<br /> echo </span><span style="color: #DD0000">"Extending class\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">displayVar</span><span style="color: #007700">();<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$extended </span><span style="color: #007700">= new </span><span style="color: #0000BB">ExtendClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$extended</span><span style="color: #007700">-></span><span style="color: #0000BB">displayVar</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>Extending classa default value</pre></div> </pre></div> </div> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.html">Classes and Objects (PHP 5)</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.autoload.html">Autoloading Objects</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects (PHP 5)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?