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:
@@ -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;
|
||||
|
@@ -5,7 +5,8 @@
|
||||
<meta charset="utf-8">
|
||||
|
||||
<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="icon" href="/favicon-32.png">
|
||||
|
@@ -11,8 +11,10 @@
|
||||
<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>
|
||||
<li><a href="<?= page_path('classes-constructor') ?>">Classes: Constructor</a></li>
|
||||
</ol>
|
||||
<a href="<?= page_path('credits') ?>">Credits</a>
|
||||
</div>
|
||||
|
@@ -10,7 +10,7 @@
|
||||
experience for the PHP programming language.
|
||||
</p>
|
||||
<p>
|
||||
Source Links:
|
||||
Sites used as a reference while writing PHP Apprentice:
|
||||
<ul>
|
||||
<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>
|
||||
@@ -27,6 +27,10 @@
|
||||
<li><a href="https://symfony.com/doc/current/frontend.html" target="_blank">Symfony Encore</a></li>
|
||||
</ul>
|
||||
</p>
|
||||
<hr />
|
||||
<p>
|
||||
phpapprentice.com was created and is managed by <a href="https://twitter.com/restoreddev">Andrew Davis</a>.
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<?php partial('table_of_contents') ?>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<?php partial('header', ['title' => $title, 'description' => $description]) ?>
|
||||
<?php partial('header', ['title' => $title, 'subtitle' => $subtitle]) ?>
|
||||
|
||||
<div class="menu">
|
||||
<button class="menu-button" title="Open Menu">
|
||||
|
47
code/classes-constructor.php
Normal file
47
code/classes-constructor.php
Normal 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');
|
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;
|
||||
}
|
||||
}
|
||||
|
27
config.php
27
config.php
@@ -15,77 +15,78 @@ return [
|
||||
Page::create('basics', null, 'basics.php', [
|
||||
'title' => 'Basics',
|
||||
'subtitle' => 'Getting started',
|
||||
'description' => 'Let us get started with the basics.',
|
||||
'next' => 'variables',
|
||||
]),
|
||||
Page::create('variables', null, 'variables.php', [
|
||||
'title' => 'Variables',
|
||||
'subtitle' => 'The building blocks of PHP',
|
||||
'description' => '',
|
||||
'previous' => 'basics',
|
||||
'next' => 'arithmetic',
|
||||
]),
|
||||
Page::create('arithmetic', null, 'arithmetic.php', [
|
||||
'title' => 'Arithmetic',
|
||||
'subtitle' => 'Doing math like a pro',
|
||||
'description' => '',
|
||||
'previous' => 'variables',
|
||||
'next' => 'comparisons',
|
||||
]),
|
||||
Page::create('comparisons', null, 'comparisons.php', [
|
||||
'title' => 'Comparisons',
|
||||
'subtitle' => 'Equality checking',
|
||||
'description' => '',
|
||||
'previous' => 'arithmetic',
|
||||
'next' => 'boolean-logic',
|
||||
]),
|
||||
Page::create('boolean-logic', null, 'boolean-logic.php', [
|
||||
'title' => 'Boolean Logic',
|
||||
'subtitle' => 'Is it a yes or a no?',
|
||||
'description' => '',
|
||||
'previous' => 'comparisons',
|
||||
'next' => 'conditionals',
|
||||
]),
|
||||
Page::create('conditionals', null, 'conditionals.php', [
|
||||
'title' => 'Conditionals',
|
||||
'subtitle' => 'Checking the if before the what',
|
||||
'description' => '',
|
||||
'previous' => 'boolean-logic',
|
||||
'next' => 'loops',
|
||||
]),
|
||||
Page::create('loops', null, 'loops.php', [
|
||||
'title' => 'Loops',
|
||||
'subtitle' => 'Increase your repetitions',
|
||||
'description' => '',
|
||||
'previous' => 'conditionals',
|
||||
'next' => 'arrays',
|
||||
]),
|
||||
Page::create('arrays', null, 'arrays.php', [
|
||||
'title' => 'Arrays',
|
||||
'subtitle' => 'Time to make a list',
|
||||
'description' => '',
|
||||
'previous' => 'loops',
|
||||
'next' => 'functions',
|
||||
]),
|
||||
Page::create('functions', null, 'functions.php', [
|
||||
'title' => 'Functions',
|
||||
'subtitle' => 'Reusable code',
|
||||
'description' => '',
|
||||
'previous' => 'arrays',
|
||||
'next' => 'classes',
|
||||
]),
|
||||
Page::create('classes', null, 'classes.php', [
|
||||
'title' => 'Classes',
|
||||
'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',
|
||||
'previous' => 'classes',
|
||||
'next' => 'classes-visibility',
|
||||
]),
|
||||
Page::create('classes-visibility', null, 'classes-visibility.php', [
|
||||
'title' => 'Classes Visibility',
|
||||
'title' => 'Classes: Visibility',
|
||||
'subtitle' => 'Privatizing your objects',
|
||||
'description' => '',
|
||||
'previous' => 'classes',
|
||||
'previous' => 'classes-inheritance',
|
||||
'next' => 'classes-constructor',
|
||||
]),
|
||||
Page::create('classes-constructor', null, 'classes-constructor.php', [
|
||||
'title' => 'Classes: Constructor',
|
||||
'subtitle' => 'Construct your objects',
|
||||
'previous' => 'classes-visibility',
|
||||
'next' => '',
|
||||
]),
|
||||
],
|
||||
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user