mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Fixed bug #51381 (Clarify class definition order importance for multiple levels of inheritance)
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@304561 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
1d0012d759
commit
962b413925
1 changed files with 91 additions and 81 deletions
|
@ -1,81 +1,91 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- $Revision$ -->
|
||||
<sect1 xml:id="language.oop5.inheritance" xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Object Inheritance</title>
|
||||
<para>
|
||||
Inheritance is a well-established programming principle, and PHP makes use
|
||||
of this principle in its object model. This principle will affect the way
|
||||
many classes and objects relate to one another.
|
||||
</para>
|
||||
<para>
|
||||
For example, when you extend a class, the subclass inherits all of the
|
||||
public and protected methods from the parent class. Unless a class overrides
|
||||
those methods, they will retain their original functionality.
|
||||
</para>
|
||||
<para>
|
||||
This is useful for defining and abstracting functionality, and permits the
|
||||
implementation of additional functionality in similar objects without the
|
||||
need to reimplement all of the shared functionality.
|
||||
</para>
|
||||
<sect2 xml:id="language.oop5.inheritance.examples">
|
||||
<example xml:id="language.oop5.inheritance.examples.ex1">
|
||||
<title>Inheritance Example</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
class foo
|
||||
{
|
||||
public function printItem($string)
|
||||
{
|
||||
echo 'Foo: ' . $string . PHP_EOL;
|
||||
}
|
||||
|
||||
public function printPHP()
|
||||
{
|
||||
echo 'PHP is great.' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
class bar extends foo
|
||||
{
|
||||
public function printItem($string)
|
||||
{
|
||||
echo 'Bar: ' . $string . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
$foo = new foo();
|
||||
$bar = new bar();
|
||||
$foo->printItem('baz'); // Output: 'Foo: baz'
|
||||
$foo->printPHP(); // Output: 'PHP is great'
|
||||
$bar->printItem('baz'); // Output: 'Bar: baz'
|
||||
$bar->printPHP(); // Output: 'PHP is great'
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect2>
|
||||
</sect1>
|
||||
|
||||
<!-- Keep this comment at the end of the file
|
||||
Local variables:
|
||||
mode: sgml
|
||||
sgml-omittag:t
|
||||
sgml-shorttag:t
|
||||
sgml-minimize-attributes:nil
|
||||
sgml-always-quote-attributes:t
|
||||
sgml-indent-step:1
|
||||
sgml-indent-data:t
|
||||
indent-tabs-mode:nil
|
||||
sgml-parent-document:nil
|
||||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||||
sgml-exposed-tags:nil
|
||||
sgml-local-catalogs:nil
|
||||
sgml-local-ecat-files:nil
|
||||
End:
|
||||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||||
vim: et tw=78 syn=sgml
|
||||
vi: ts=1 sw=1
|
||||
-->
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- $Revision$ -->
|
||||
<sect1 xml:id="language.oop5.inheritance" xmlns="http://docbook.org/ns/docbook">
|
||||
<title>Object Inheritance</title>
|
||||
<para>
|
||||
Inheritance is a well-established programming principle, and PHP makes use
|
||||
of this principle in its object model. This principle will affect the way
|
||||
many classes and objects relate to one another.
|
||||
</para>
|
||||
<para>
|
||||
For example, when you extend a class, the subclass inherits all of the
|
||||
public and protected methods from the parent class. Unless a class overrides
|
||||
those methods, they will retain their original functionality.
|
||||
</para>
|
||||
<para>
|
||||
This is useful for defining and abstracting functionality, and permits the
|
||||
implementation of additional functionality in similar objects without the
|
||||
need to reimplement all of the shared functionality.
|
||||
</para>
|
||||
|
||||
<sect2 xml:id="language.oop5.inheritance.examples">
|
||||
<example xml:id="language.oop5.inheritance.examples.ex1">
|
||||
<title>Inheritance Example</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
|
||||
class foo
|
||||
{
|
||||
public function printItem($string)
|
||||
{
|
||||
echo 'Foo: ' . $string . PHP_EOL;
|
||||
}
|
||||
|
||||
public function printPHP()
|
||||
{
|
||||
echo 'PHP is great.' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
class bar extends foo
|
||||
{
|
||||
public function printItem($string)
|
||||
{
|
||||
echo 'Bar: ' . $string . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
$foo = new foo();
|
||||
$bar = new bar();
|
||||
$foo->printItem('baz'); // Output: 'Foo: baz'
|
||||
$foo->printPHP(); // Output: 'PHP is great'
|
||||
$bar->printItem('baz'); // Output: 'Bar: baz'
|
||||
$bar->printPHP(); // Output: 'PHP is great'
|
||||
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</example>
|
||||
</sect2>
|
||||
|
||||
<note>
|
||||
<para>
|
||||
Unless autoloading is used, then classes must be defined before they are
|
||||
used. If a class extends another, then the parent class must be declared
|
||||
before the child class structure. This rule applies to class that inherit
|
||||
other classes and interfaces.
|
||||
</para>
|
||||
</note>
|
||||
</sect1>
|
||||
|
||||
<!-- Keep this comment at the end of the file
|
||||
Local variables:
|
||||
mode: sgml
|
||||
sgml-omittag:t
|
||||
sgml-shorttag:t
|
||||
sgml-minimize-attributes:nil
|
||||
sgml-always-quote-attributes:t
|
||||
sgml-indent-step:1
|
||||
sgml-indent-data:t
|
||||
indent-tabs-mode:nil
|
||||
sgml-parent-document:nil
|
||||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||||
sgml-exposed-tags:nil
|
||||
sgml-local-catalogs:nil
|
||||
sgml-local-ecat-files:nil
|
||||
End:
|
||||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||||
vim: et tw=78 syn=sgml
|
||||
vi: ts=1 sw=1
|
||||
-->
|
||||
|
|
Loading…
Reference in a new issue