1
0
mirror of https://github.com/restoreddev/phpapprentice.git synced 2025-08-06 06:47:58 +02:00

Merge pull request #5 from restoreddev/classes

Adding more pages about classes
This commit is contained in:
Andrew
2018-09-03 11:55:41 -05:00
committed by GitHub
10 changed files with 164 additions and 21 deletions

View File

@@ -28,6 +28,11 @@ a:hover {
text-decoration: underline; text-decoration: underline;
color: $primary-color-dark; color: $primary-color-dark;
} }
hr {
border: 0;
height: 1px;
background-color: #EEE;
}
p { p {
max-width: 40em; max-width: 40em;
line-height: 1.5; line-height: 1.5;

View File

@@ -5,7 +5,8 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title><?= $title ?? 'PHP Apprentice' ?></title> <title><?= $title ?? 'PHP Apprentice' ?></title>
<meta name="description" content="<?= $description ?? 'A site for learning how to use PHP'?>"> <meta name="description" content="<?= $subtitle ?? 'A site for learning how to use PHP' ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/css/site.css"> <link rel="stylesheet" href="/css/site.css">
<link rel="icon" href="/favicon-32.png"> <link rel="icon" href="/favicon-32.png">

View File

@@ -11,8 +11,10 @@
<li><a href="<?= page_path('loops') ?>">Loops</a></li> <li><a href="<?= page_path('loops') ?>">Loops</a></li>
<li><a href="<?= page_path('arrays') ?>">Arrays</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('functions') ?>">Functions</a></li>
<li><a href="<?= page_path('classes') ?>">Classes</a></li> <li><a href="<?= page_path('classes') ?>">Classes: Introduction</a></li>
<li><a href="<?= page_path('classes-visibility') ?>">Classes - Visibility</a></li> <li><a href="<?= page_path('classes-inheritance') ?>">Classes: Inheritance</a></li>
<li><a href="<?= page_path('classes-visibility') ?>">Classes: Visibility</a></li>
<li><a href="<?= page_path('classes-constructor') ?>">Classes: Constructor</a></li>
</ol> </ol>
<a href="<?= page_path('credits') ?>">Credits</a> <a href="<?= page_path('credits') ?>">Credits</a>
</div> </div>

View File

@@ -10,7 +10,7 @@
experience for the PHP programming language. experience for the PHP programming language.
</p> </p>
<p> <p>
Source Links: Sites used as a reference while writing PHP Apprentice:
<ul> <ul>
<li><a href="https://secure.php.net/" target="_blank">php.net</a></li> <li><a href="https://secure.php.net/" target="_blank">php.net</a></li>
<li><a href="https://www.phptherightway.com/" target="_blank">PHP The Right Way</a></li> <li><a href="https://www.phptherightway.com/" target="_blank">PHP The Right Way</a></li>
@@ -27,6 +27,10 @@
<li><a href="https://symfony.com/doc/current/frontend.html" target="_blank">Symfony Encore</a></li> <li><a href="https://symfony.com/doc/current/frontend.html" target="_blank">Symfony Encore</a></li>
</ul> </ul>
</p> </p>
<hr />
<p>
phpapprentice.com was created and is managed by <a href="https://twitter.com/restoreddev">Andrew Davis</a>.
</p>
</div> </div>
<div> <div>
<?php partial('table_of_contents') ?> <?php partial('table_of_contents') ?>

View File

@@ -1,4 +1,4 @@
<?php partial('header', ['title' => $title, 'description' => $description]) ?> <?php partial('header', ['title' => $title, 'subtitle' => $subtitle]) ?>
<div class="menu"> <div class="menu">
<button class="menu-button" title="Open Menu"> <button class="menu-button" title="Open Menu">

View File

@@ -0,0 +1,47 @@
<?php
// Whenever you create an object in PHP, you put parentheses after the class name.
// In the previous examples, we always left the parentheses empty.
class Hat {
public $color;
public function setColor($color)
{
$this->color = $color;
}
}
$hat = new Hat();
// However, you can actually pass data into the parentheses like a function.
// The data will be passed to a special function on the class called a constructor.
class Ballcap
{
public $color;
public function __construct($color )
{
$this->color = $color;
}
}
// A constructor is not required, but can make creating a new object easier.
// They are usually used to define the initial value of a property.
// Instead of writing:
$hat = new Hat();
$hat->setColor('Red');
// You can write:
$ballcap = new Ballcap('Blue');
// Constructors do not return values because the return value is a always a new object.
class Tophat
{
public function __construct($color)
{
return $color;
}
}
// "$tophat" now holds an instance of Tophat, not the color "Grey".
$tophat = new Tophat('Grey');

View 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();

View File

@@ -33,11 +33,28 @@ class Phone2
} }
} }
// The "protected" and "private" keywords work a little differently, but we // The "protected" and "private" keywords work a little differently.
// will learn more about "protected" when we discuss inheritance. // They both prevent functions and properties from being accessed outside an object.
// However, 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 class Phone3
{ {
private $number; private $number;
protected $caller; 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;
}
} }

View File

@@ -15,77 +15,78 @@ return [
Page::create('basics', null, 'basics.php', [ Page::create('basics', null, 'basics.php', [
'title' => 'Basics', 'title' => 'Basics',
'subtitle' => 'Getting started', 'subtitle' => 'Getting started',
'description' => 'Let us get started with the basics.',
'next' => 'variables', 'next' => 'variables',
]), ]),
Page::create('variables', null, 'variables.php', [ Page::create('variables', null, 'variables.php', [
'title' => 'Variables', 'title' => 'Variables',
'subtitle' => 'The building blocks of PHP', 'subtitle' => 'The building blocks of PHP',
'description' => '',
'previous' => 'basics', 'previous' => 'basics',
'next' => 'arithmetic', 'next' => 'arithmetic',
]), ]),
Page::create('arithmetic', null, 'arithmetic.php', [ Page::create('arithmetic', null, 'arithmetic.php', [
'title' => 'Arithmetic', 'title' => 'Arithmetic',
'subtitle' => 'Doing math like a pro', 'subtitle' => 'Doing math like a pro',
'description' => '',
'previous' => 'variables', 'previous' => 'variables',
'next' => 'comparisons', 'next' => 'comparisons',
]), ]),
Page::create('comparisons', null, 'comparisons.php', [ Page::create('comparisons', null, 'comparisons.php', [
'title' => 'Comparisons', 'title' => 'Comparisons',
'subtitle' => 'Equality checking', 'subtitle' => 'Equality checking',
'description' => '',
'previous' => 'arithmetic', 'previous' => 'arithmetic',
'next' => 'boolean-logic', 'next' => 'boolean-logic',
]), ]),
Page::create('boolean-logic', null, 'boolean-logic.php', [ Page::create('boolean-logic', null, 'boolean-logic.php', [
'title' => 'Boolean Logic', 'title' => 'Boolean Logic',
'subtitle' => 'Is it a yes or a no?', 'subtitle' => 'Is it a yes or a no?',
'description' => '',
'previous' => 'comparisons', 'previous' => 'comparisons',
'next' => 'conditionals', 'next' => 'conditionals',
]), ]),
Page::create('conditionals', null, 'conditionals.php', [ Page::create('conditionals', null, 'conditionals.php', [
'title' => 'Conditionals', 'title' => 'Conditionals',
'subtitle' => 'Checking the if before the what', 'subtitle' => 'Checking the if before the what',
'description' => '',
'previous' => 'boolean-logic', 'previous' => 'boolean-logic',
'next' => 'loops', 'next' => 'loops',
]), ]),
Page::create('loops', null, 'loops.php', [ Page::create('loops', null, 'loops.php', [
'title' => 'Loops', 'title' => 'Loops',
'subtitle' => 'Increase your repetitions', 'subtitle' => 'Increase your repetitions',
'description' => '',
'previous' => 'conditionals', 'previous' => 'conditionals',
'next' => 'arrays', 'next' => 'arrays',
]), ]),
Page::create('arrays', null, 'arrays.php', [ Page::create('arrays', null, 'arrays.php', [
'title' => 'Arrays', 'title' => 'Arrays',
'subtitle' => 'Time to make a list', 'subtitle' => 'Time to make a list',
'description' => '',
'previous' => 'loops', 'previous' => 'loops',
'next' => 'functions', 'next' => 'functions',
]), ]),
Page::create('functions', null, 'functions.php', [ Page::create('functions', null, 'functions.php', [
'title' => 'Functions', 'title' => 'Functions',
'subtitle' => 'Reusable code', 'subtitle' => 'Reusable code',
'description' => '',
'previous' => 'arrays', 'previous' => 'arrays',
'next' => 'classes', 'next' => 'classes',
]), ]),
Page::create('classes', null, 'classes.php', [ Page::create('classes', null, 'classes.php', [
'title' => 'Classes', 'title' => 'Classes',
'subtitle' => 'Object-oriented programming', 'subtitle' => 'Object-oriented programming',
'description' => '',
'previous' => 'functions', 'previous' => 'functions',
'next' => 'classes-inheritance',
]),
Page::create('classes-inheritance', null, 'classes-inheritance.php', [
'title' => 'Classes: Inheritance',
'subtitle' => 'Extend your objects',
'previous' => 'classes',
'next' => 'classes-visibility', 'next' => 'classes-visibility',
]), ]),
Page::create('classes-visibility', null, 'classes-visibility.php', [ Page::create('classes-visibility', null, 'classes-visibility.php', [
'title' => 'Classes Visibility', 'title' => 'Classes: Visibility',
'subtitle' => 'Privatizing your objects', 'subtitle' => 'Privatizing your objects',
'description' => '', 'previous' => 'classes-inheritance',
'previous' => 'classes', 'next' => 'classes-constructor',
]),
Page::create('classes-constructor', null, 'classes-constructor.php', [
'title' => 'Classes: Constructor',
'subtitle' => 'Construct your objects',
'previous' => 'classes-visibility',
'next' => '', 'next' => '',
]), ]),
], ],

View File

@@ -96,6 +96,10 @@ class Build
private function buildPage(Page $page): string private function buildPage(Page $page): string
{ {
if (!empty($page->code)) { 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); $code = file_get_contents(config('code_dir') . '/' . $page->code);
$page->variables['code'] = $code; $page->variables['code'] = $code;
} }