From e404ab5489da14e3ec2b3585916496f7862696e3 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Sun, 2 Sep 2018 23:09:35 -0500 Subject: [PATCH 1/5] Working on inheritance page --- assets/templates/_table_of_contents.phtml | 5 +- code/classes-inheritance.php | 62 +++++++++++++++++++++++ code/classes-visibility.php | 23 +++++++-- config.php | 9 +++- 4 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 code/classes-inheritance.php diff --git a/assets/templates/_table_of_contents.phtml b/assets/templates/_table_of_contents.phtml index d6b368f..21c04f8 100644 --- a/assets/templates/_table_of_contents.phtml +++ b/assets/templates/_table_of_contents.phtml @@ -11,8 +11,9 @@
  • Loops
  • Arrays
  • Functions
  • -
  • Classes
  • -
  • Classes - Visibility
  • +
  • Classes: Introduction
  • +
  • Classes: Inheritance
  • +
  • Classes: Visibility
  • Credits diff --git a/code/classes-inheritance.php b/code/classes-inheritance.php new file mode 100644 index 0000000..f1eed47 --- /dev/null +++ b/code/classes-inheritance.php @@ -0,0 +1,62 @@ +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(); diff --git a/code/classes-visibility.php b/code/classes-visibility.php index 24e1d90..2833608 100644 --- a/code/classes-visibility.php +++ b/code/classes-visibility.php @@ -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; + } } diff --git a/config.php b/config.php index f3742b9..39b7303 100644 --- a/config.php +++ b/config.php @@ -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' => '', ]), ], From 5af12b3ea8a5470a7cb114712be42cf4178bf373 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Mon, 3 Sep 2018 11:23:01 -0500 Subject: [PATCH 2/5] Added to credits page --- assets/css/site.css | 5 +++++ assets/templates/credits.phtml | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/assets/css/site.css b/assets/css/site.css index 18ac72d..fe19c8f 100644 --- a/assets/css/site.css +++ b/assets/css/site.css @@ -28,6 +28,11 @@ a:hover { text-decoration: underline; color: $primary-color-dark; } +hr { + border: 0; + height: 1px; + background-color: #EEE; +} p { max-width: 40em; line-height: 1.5; diff --git a/assets/templates/credits.phtml b/assets/templates/credits.phtml index 71e3bf9..730715a 100644 --- a/assets/templates/credits.phtml +++ b/assets/templates/credits.phtml @@ -10,7 +10,7 @@ experience for the PHP programming language.

    - Source Links: + Sites used as a reference while writing PHP Apprentice:

    +
    +

    + phpapprentice.com was created and is managed by Andrew Davis. +

    From f30f79c776ed56e6c51b55b07e00b0db16d8b281 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Mon, 3 Sep 2018 11:23:26 -0500 Subject: [PATCH 3/5] Added viewport meta tag to header --- assets/templates/_header.phtml | 1 + 1 file changed, 1 insertion(+) diff --git a/assets/templates/_header.phtml b/assets/templates/_header.phtml index ecf8755..f419bbf 100644 --- a/assets/templates/_header.phtml +++ b/assets/templates/_header.phtml @@ -6,6 +6,7 @@ <?= $title ?? 'PHP Apprentice' ?> + From bf108af8e39082f4591df8c115f95b8127b7ff9f Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Mon, 3 Sep 2018 11:35:21 -0500 Subject: [PATCH 4/5] Added exception throw when code file cannot be found --- src/Build.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Build.php b/src/Build.php index 2dda936..e239914 100644 --- a/src/Build.php +++ b/src/Build.php @@ -96,6 +96,10 @@ class Build private function buildPage(Page $page): string { if (!empty($page->code)) { + if (!file_exists(config('code_dir') . '/' . $page->code)) { + throw new \Exception('Code file not found: ' . $page->code); + } + $code = file_get_contents(config('code_dir') . '/' . $page->code); $page->variables['code'] = $code; } From 80a4cb5f4b25b836abf6b3e13fe49a9037d946c5 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Mon, 3 Sep 2018 11:52:41 -0500 Subject: [PATCH 5/5] Added classes-constructor and removed description from templates --- assets/templates/_header.phtml | 2 +- assets/templates/_table_of_contents.phtml | 1 + assets/templates/default.phtml | 2 +- code/classes-constructor.php | 47 +++++++++++++++++++++++ config.php | 22 ++++------- 5 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 code/classes-constructor.php diff --git a/assets/templates/_header.phtml b/assets/templates/_header.phtml index f419bbf..9b8a2c8 100644 --- a/assets/templates/_header.phtml +++ b/assets/templates/_header.phtml @@ -5,7 +5,7 @@ <?= $title ?? 'PHP Apprentice' ?> - + diff --git a/assets/templates/_table_of_contents.phtml b/assets/templates/_table_of_contents.phtml index 21c04f8..c6d9ad9 100644 --- a/assets/templates/_table_of_contents.phtml +++ b/assets/templates/_table_of_contents.phtml @@ -14,6 +14,7 @@
  • Classes: Introduction
  • Classes: Inheritance
  • Classes: Visibility
  • +
  • Classes: Constructor
  • Credits
    diff --git a/assets/templates/default.phtml b/assets/templates/default.phtml index b91a3d4..c6fcab0 100644 --- a/assets/templates/default.phtml +++ b/assets/templates/default.phtml @@ -1,4 +1,4 @@ - $title, 'description' => $description]) ?> + $title, 'subtitle' => $subtitle]) ?>