Source of: /manual/en/language.oop5.basic.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.oop5.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'language.oop5.basic.php',
1 => 'The Basics',
),
'up' =>
array (
0 => 'language.oop5.php',
1 => 'Classes and Objects',
),
'prev' =>
array (
0 => 'oop5.intro.php',
1 => 'Introduction',
),
'next' =>
array (
0 => 'language.oop5.properties.php',
1 => 'Properties',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="language.oop5.basic" class="sect1">
<h2 class="title">The Basics</h2>
<div id="language.oop5.basic.class" class="sect2">
<h3 class="title">class</h3>
<p class="para">
Every class definition begins with the
keyword <i>class</i>, followed by a class name,
followed by a pair of curly braces which enclose the definitions
of the class's properties and methods.
</p>
<p class="para">
The class name can be any valid label which is a not a
PHP <a href="reserved.php" class="link">reserved word</a>. A valid class
name starts with a letter or underscore, followed by any number of
letters, numbers, or underscores. As a regular expression, it
would be expressed thus:
<i>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*</i>.
</p>
<p class="para">
A class may contain its
own <a href="language.oop5.constants.php" class="link">constants</a>, <a href="language.oop5.properties.php" class="link">variables</a>
(called "properties"), and functions (called "methods").
</p>
<div class="example">
<p><b>Example #1 Simple Class definition</b></p>
<div class="example-contents programlisting">
<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">SimpleClass<br /></span><span style="color: #007700">{<br /> </span><span style="color: #FF8000">// property declaration<br /> </span><span style="color: #007700">public </span><span style="color: #0000BB">$var </span><span style="color: #007700">= </span><span style="color: #DD0000">'a default value'</span><span style="color: #007700">;<br /><br /> </span><span style="color: #FF8000">// method declaration<br /> </span><span style="color: #007700">public function </span><span style="color: #0000BB">displayVar</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">var</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div>
<p class="para">
The pseudo-variable <var class="varname">$this</var> is available when a
method is called from within an object
context. <var class="varname">$this</var> is a reference to the calling
object (usually the object to which the method belongs, but
possibly another object, if the method is called
<a href="language.oop5.static.php" class="link">statically</a> from the context
of a secondary object).
</p>
<p class="para">
</p><div class="example">
<p><b>Example #2 Some examples of the <var class="varname">$this</var> pseudo-variable</b></p>
<div class="example-contents programlisting">
<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">A<br /></span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">foo</span><span style="color: #007700">()<br /> {<br /> if (isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">'$this is defined ('</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">);<br /> echo </span><span style="color: #DD0000">")\n"</span><span style="color: #007700">;<br /> } else {<br /> echo </span><span style="color: #DD0000">"\$this is not defined.\n"</span><span style="color: #007700">;<br /> }<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B<br /></span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">bar</span><span style="color: #007700">()<br /> {<br /> </span><span style="color: #FF8000">// Note: the next line will issue a warning if E_STRICT is enabled.<br /> </span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">A</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">-></span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Note: the next line will issue a warning if E_STRICT is enabled.<br /></span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= new </span><span style="color: #0000BB">B</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-></span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Note: the next line will issue a warning if E_STRICT is enabled.<br /></span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.
</pre></div>
</div>
</div><p>
</p>
</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.php" class="link">constructor</a> defined that throws an
<a href="language.exceptions.php" 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 #3 Creating an instance</b></p>
<div class="example-contents programlisting">
<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 /><br /></span><span style="color: #FF8000">// This can also be done with a variable:<br /></span><span style="color: #0000BB">$className </span><span style="color: #007700">= </span><span style="color: #DD0000">'Foo'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$instance </span><span style="color: #007700">= new </span><span style="color: #0000BB">$className</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Foo()<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.php" class="link">cloning</a> it.
</p>
<div class="example">
<p><b>Example #4 Object Assignment</b></p>
<div class="example-contents programlisting">
<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 para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
NULL
NULL
object(SimpleClass)#1 (1) {
["var"]=>
string(30) "$assigned will have this value"
}
</pre></div>
</div>
</div>
</div>
<div id="language.oop5.basic.extends" class="sect2">
<h3 class="title">extends</h3>
<p class="para">
A class can inherit the methods and properties of another class by
using the keyword <i>extends</i> in the class
declaration. It is not possible to extend multiple classes; a
class can only inherit from one base class.
</p>
<p class="para">
The inherited methods and properties can be overridden by
redeclaring them with the same name defined in the parent
class. However, if the parent class has defined a method
as <a href="language.oop5.final.php" class="link">final</a>, that method
may not be overridden. It is possible to access the overridden
methods or static properties by referencing them
with <a href="language.oop5.paamayim-nekudotayim.php" class="link">parent::</a>.
</p>
<div class="example">
<p><b>Example #5 Simple Class Inheritance</b></p>
<div class="example-contents programlisting">
<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 para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Extending class
a default value
</pre></div>
</div>
</div>
</div>
</div><?php manual_footer(); ?>