mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-07-30 19:40:43 +02:00
Working on inheritance page
This commit is contained in:
@@ -11,8 +11,9 @@
|
||||
<li><a href="<?= page_path('loops') ?>">Loops</a></li>
|
||||
<li><a href="<?= page_path('arrays') ?>">Arrays</a></li>
|
||||
<li><a href="<?= page_path('functions') ?>">Functions</a></li>
|
||||
<li><a href="<?= page_path('classes') ?>">Classes</a></li>
|
||||
<li><a href="<?= page_path('classes-visibility') ?>">Classes - Visibility</a></li>
|
||||
<li><a href="<?= page_path('classes') ?>">Classes: Introduction</a></li>
|
||||
<li><a href="<?= page_path('classes-inheritance') ?>">Classes: Inheritance</a></li>
|
||||
<li><a href="<?= page_path('classes-visibility') ?>">Classes: Visibility</a></li>
|
||||
</ol>
|
||||
<a href="<?= page_path('credits') ?>">Credits</a>
|
||||
</div>
|
||||
|
62
code/classes-inheritance.php
Normal file
62
code/classes-inheritance.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
// In PHP, a class can extend another class, inheriting the parent class'
|
||||
// properties and methods. To make a class a child of another, use the "extends"
|
||||
// keyword after the class name.
|
||||
class Vehicle
|
||||
{
|
||||
public function drive()
|
||||
{
|
||||
echo "driving...\n";
|
||||
}
|
||||
}
|
||||
|
||||
class Truck extends Vehicle {}
|
||||
|
||||
// This does not error because "Truck" extends "Vehicle".
|
||||
$truck = new Truck();
|
||||
$truck->drive();
|
||||
|
||||
// Even though the child class inherits a parent class' properties and methods,
|
||||
// the child can still override the parent.
|
||||
class Tractor extends Vehicle
|
||||
{
|
||||
public function drive()
|
||||
{
|
||||
echo "driving slowly...\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Outputs "driving slowly..." instead of "driving...".
|
||||
$tractor = new Tractor();
|
||||
$tractor->drive();
|
||||
|
||||
// A class can use a parent's property or method from the "$this" variable.
|
||||
class Motorcycle extends Vehicle
|
||||
{
|
||||
public function pushPedal()
|
||||
{
|
||||
$this->drive();
|
||||
}
|
||||
}
|
||||
|
||||
// Outputs "driving...".
|
||||
$cycle = new Motorcycle();
|
||||
$cycle->pushPedal();
|
||||
|
||||
// If you override a parent's property or method, the "$this" variable will refer to the child's
|
||||
// implementation of the property or method. To call the parent's property or method explicity,
|
||||
// use the "parent" keyword.
|
||||
class Racecar extends Vehicle
|
||||
{
|
||||
public function drive()
|
||||
{
|
||||
parent::drive();
|
||||
|
||||
echo "driving even faster...\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Outputs "driving..." and "driving even faster...".
|
||||
$racecar = new Racecar();
|
||||
$racecar->drive();
|
@@ -33,11 +33,28 @@ class Phone2
|
||||
}
|
||||
}
|
||||
|
||||
// The "protected" and "private" keywords work a little differently, but we
|
||||
// will learn more about "protected" when we discuss inheritance.
|
||||
// However, they both prevent functions and properties from being accessed outside an object.
|
||||
// The "protected" and "private" keywords work a little differently.
|
||||
// They both prevent functions and properties from being accessed outside an object.
|
||||
// However, a method or property marked "protected" can still be accessed by a child object.
|
||||
class Phone3
|
||||
{
|
||||
private $number;
|
||||
|
||||
protected $caller;
|
||||
|
||||
public function setNumber($number)
|
||||
{
|
||||
$this->number = $number;
|
||||
}
|
||||
}
|
||||
|
||||
// In class "Smartphone", the "caller" property is accessible because the parent class
|
||||
// has it marked as "protected". However, "Smartphone" cannot access the the "number" property
|
||||
// because it is still listed as private.
|
||||
class Smartphone extends Phone3
|
||||
{
|
||||
public function setCaller($caller)
|
||||
{
|
||||
$this->caller = $caller;
|
||||
}
|
||||
}
|
||||
|
@@ -79,13 +79,20 @@ return [
|
||||
'subtitle' => 'Object-oriented programming',
|
||||
'description' => '',
|
||||
'previous' => 'functions',
|
||||
'next' => 'classes-inheritance',
|
||||
]),
|
||||
Page::create('classes-inheritance', null, 'classes-inheritance.php', [
|
||||
'title' => 'Classes Inheritance',
|
||||
'subtitle' => 'Extend your objects',
|
||||
'description' => '',
|
||||
'previous' => 'classes',
|
||||
'next' => 'classes-visibility',
|
||||
]),
|
||||
Page::create('classes-visibility', null, 'classes-visibility.php', [
|
||||
'title' => 'Classes Visibility',
|
||||
'subtitle' => 'Privatizing your objects',
|
||||
'description' => '',
|
||||
'previous' => 'classes',
|
||||
'previous' => 'classes-inheritance',
|
||||
'next' => '',
|
||||
]),
|
||||
],
|
||||
|
Reference in New Issue
Block a user