php-doc-en/reference/classobj/examples.xml
Kamil Tekiela 6f11457f11
New examples for classes (#750)
Co-authored-by: Anna Filina <afilina@gmail.com>
2021-08-05 21:37:30 +01:00

191 lines
4 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<appendix xml:id="classobj.examples">
&reftitle.examples;
<para>
In this example, we first define a base class and an extension
of the class. The base class describes a general vegetable,
whether it is edible, and what is its color. The subclass
<varname>Spinach</varname> adds a method to cook it and another to
find out if it is cooked.
</para>
<para>
<example>
<title>Class Definitions</title>
<para><varname>Vegetable</varname></para>
<programlisting role="php">
<![CDATA[
<?php
class Vegetable {
public $edible;
public $color;
public function __construct($edible, $color = "green")
{
$this->edible = $edible;
$this->color = $color;
}
public function isEdible()
{
return $this->edible;
}
public function getColor()
{
return $this->color;
}
}
?>
]]>
</programlisting>
<para><varname>Spinach</varname></para>
<programlisting role="php">
<![CDATA[
<?php
class Spinach extends Vegetable {
public $cooked = false;
public function __construct()
{
parent::__construct(true, "green");
}
public function cook()
{
$this->cooked = true;
}
public function isCooked()
{
return $this->cooked;
}
}
?>
]]>
</programlisting>
</example>
</para>
<para>
We then instantiate 2 objects from these classes and print out
information about them, including their class parentage.
We also define some utility functions, mainly to have a nice printout
of the variables.
</para>
<para>
<example>
<title>test_script.php</title>
<programlisting role="php">
<![CDATA[
<?php
// register autoloader to load classes
spl_autoload_register();
function printProperties($obj)
{
foreach (get_object_vars($obj) as $prop => $val) {
echo "\t$prop = $val\n";
}
}
function printMethods($obj)
{
$arr = get_class_methods(get_class($obj));
foreach ($arr as $method) {
echo "\tfunction $method()\n";
}
}
function objectBelongsTo($obj, $class)
{
if (is_subclass_of($obj, $class)) {
echo "Object belongs to class " . get_class($obj);
echo ", a subclass of $class\n";
} else {
echo "Object does not belong to a subclass of $class\n";
}
}
// instantiate 2 objects
$veggie = new Vegetable(true, "blue");
$leafy = new Spinach();
// print out information about objects
echo "veggie: CLASS " . get_class($veggie) . "\n";
echo "leafy: CLASS " . get_class($leafy);
echo ", PARENT " . get_parent_class($leafy) . "\n";
// show veggie properties
echo "\nveggie: Properties\n";
printProperties($veggie);
// and leafy methods
echo "\nleafy: Methods\n";
printMethods($leafy);
echo "\nParentage:\n";
objectBelongsTo($leafy, Spinach::class);
objectBelongsTo($leafy, Vegetable::class);
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
veggie: CLASS Vegetable
leafy: CLASS Spinach, PARENT Vegetable
veggie: Properties
edible = 1
color = blue
leafy: Methods
function __construct()
function cook()
function isCooked()
function isEdible()
function getColor()
Parentage:
Object does not belong to a subclass of Spinach
Object belongs to class Spinach, a subclass of Vegetable
]]>
</screen>
<para>
One important thing to note in the example above is that
the object <varname>$leafy</varname> is an instance of the class
<classname>Spinach</classname> which is a subclass of
<classname>Vegetable</classname>.
</para>
</example>
</para>
</appendix>
<!-- 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
-->