New examples for classes (#750)

Co-authored-by: Anna Filina <afilina@gmail.com>
This commit is contained in:
Kamil Tekiela 2021-08-05 21:37:30 +01:00 committed by GitHub
parent 43ac336818
commit 6f11457f11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,62 +6,66 @@
<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 or not and what is its color. The subclass
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>classes.inc</title>
<title>Class Definitions</title>
<para><varname>Vegetable</varname></para>
<programlisting role="php">
<![CDATA[
<?php
// base class with member properties and methods
class Vegetable {
public $edible;
var $edible;
var $color;
public $color;
function __construct($edible, $color="green")
{
$this->edible = $edible;
$this->color = $color;
}
public function __construct($edible, $color = "green")
{
$this->edible = $edible;
$this->color = $color;
}
function is_edible()
{
return $this->edible;
}
public function isEdible()
{
return $this->edible;
}
function what_color()
{
return $this->color;
}
public function getColor()
{
return $this->color;
}
}
} // end of class Vegetable
?>
]]>
</programlisting>
<para><varname>Spinach</varname></para>
<programlisting role="php">
<![CDATA[
<?php
// extends the base class
class Spinach extends Vegetable {
public $cooked = false;
var $cooked = false;
public function __construct()
{
parent::__construct(true, "green");
}
function __construct()
{
parent::__construct(true, "green");
}
public function cook()
{
$this->cooked = true;
}
function cook_it()
{
$this->cooked = true;
}
function is_cooked()
{
return $this->cooked;
}
} // end of class Spinach
public function isCooked()
{
return $this->cooked;
}
}
?>
]]>
@ -79,40 +83,37 @@ class Spinach extends Vegetable {
<title>test_script.php</title>
<programlisting role="php">
<![CDATA[
<pre>
<?php
include "classes.inc";
// register autoloader to load classes
spl_autoload_register();
// utility functions
function print_vars($obj)
function printProperties($obj)
{
foreach (get_object_vars($obj) as $prop => $val) {
echo "\t$prop = $val\n";
}
foreach (get_object_vars($obj) as $prop => $val) {
echo "\t$prop = $val\n";
}
}
function print_methods($obj)
function printMethods($obj)
{
$arr = get_class_methods(get_class($obj));
foreach ($arr as $method) {
echo "\tfunction $method()\n";
}
$arr = get_class_methods(get_class($obj));
foreach ($arr as $method) {
echo "\tfunction $method()\n";
}
}
function class_parentage($obj, $class)
function objectBelongsTo($obj, $class)
{
if (is_subclass_of($GLOBALS[$obj], $class)) {
echo "Object $obj belongs to class " . get_class($GLOBALS[$obj]);
echo ", a subclass of $class\n";
} else {
echo "Object $obj does not belong to a subclass of $class\n";
}
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();
@ -123,34 +124,47 @@ echo ", PARENT " . get_parent_class($leafy) . "\n";
// show veggie properties
echo "\nveggie: Properties\n";
print_vars($veggie);
printProperties($veggie);
// and leafy methods
echo "\nleafy: Methods\n";
print_methods($leafy);
printMethods($leafy);
echo "\nParentage:\n";
class_parentage("leafy", "Spinach");
class_parentage("leafy", "Vegetable");
objectBelongsTo($leafy, Spinach::class);
objectBelongsTo($leafy, Vegetable::class);
?>
</pre>
]]>
</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>,
therefore the last part of the script above will output:
<classname>Vegetable</classname>.
</para>
<screen>
<![CDATA[
[...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach, a subclass of Vegetable
]]>
</screen>
</example>
</para>
</appendix>
@ -175,4 +189,3 @@ vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->