mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-08-04 13:57:40 +02:00
Merge pull request #9 from restoreddev/classes
Updates to classes pages and new strings page
This commit is contained in:
@@ -5,16 +5,18 @@
|
||||
<li><a href="<?= page_path('basics') ?>">Basics</a></li>
|
||||
<li><a href="<?= page_path('variables') ?>">Variables</a></li>
|
||||
<li><a href="<?= page_path('arithmetic') ?>">Arithmetic</a></li>
|
||||
<li><a href="<?= page_path('strings') ?>">Strings</a></li>
|
||||
<li><a href="<?= page_path('comparisons') ?>">Comparisons</a></li>
|
||||
<li><a href="<?= page_path('boolean-logic') ?>">Boolean Logic</a></li>
|
||||
<li><a href="<?= page_path('conditionals') ?>">Conditionals</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('functions') ?>">Functions</a></li>
|
||||
<li><a href="<?= page_path('classes') ?>">Classes: Introduction</a></li>
|
||||
<li><a href="<?= page_path('classes') ?>">Classes</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>
|
||||
<li><a href="<?= page_path('static') ?>">Static</a></li>
|
||||
</ol>
|
||||
<a href="<?= page_path('credits') ?>">Credits</a>
|
||||
</div>
|
||||
|
@@ -8,18 +8,16 @@ echo "Hello World!\n";
|
||||
// There is a lot going on in this statement so let's work through it.
|
||||
|
||||
// First, the echo keyword tells PHP to output some text.
|
||||
echo 'I am some text';
|
||||
echo "I am some text\n";
|
||||
|
||||
// Second, PHP stores text in strings.
|
||||
|
||||
// To write a string, you surround letters with single or double quotes.
|
||||
// The difference between single quoted strings and double quoted strings
|
||||
// is that double quoted strings can hold special characters like \n which tells PHP to start a new line.
|
||||
'I am a string';
|
||||
// Double quoted strings can hold special characters like \n which tells PHP to start a new line.
|
||||
"\nI am a string on a new line";
|
||||
|
||||
// Third, all lines of code in PHP must end in a semi-colon.
|
||||
echo "No semi-colon is a no-no\n";
|
||||
|
||||
// Using semi-colons means we can write multiple statements on one line.
|
||||
echo 'Hello'; echo " World\n";
|
||||
echo "Hello"; echo " World\n";
|
||||
|
@@ -27,7 +27,7 @@ class Tractor extends Vehicle
|
||||
}
|
||||
}
|
||||
|
||||
// Outputs "driving slowly..." instead of "driving...".
|
||||
// The drive function now outputs "driving slowly..." instead of "driving...".
|
||||
$tractor = new Tractor();
|
||||
$tractor->drive();
|
||||
|
||||
|
@@ -35,7 +35,7 @@ class Phone2
|
||||
|
||||
// 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.
|
||||
// However, a method or property marked "protected" can still be accessed by a child class.
|
||||
class Phone3
|
||||
{
|
||||
private $number;
|
||||
@@ -49,7 +49,7 @@ class Phone3
|
||||
}
|
||||
|
||||
// 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
|
||||
// has it marked as "protected". However, "Smartphone" cannot access the "number" property
|
||||
// because it is still listed as private.
|
||||
class Smartphone extends Phone3
|
||||
{
|
||||
|
@@ -43,6 +43,7 @@ class Tricycle
|
||||
}
|
||||
}
|
||||
|
||||
// You can execute a method on an object using the same "->" arrow characters.
|
||||
$bike = new Tricycle();
|
||||
$bike->color = 'Red';
|
||||
$bike->echoColor();
|
||||
|
70
code/static.php
Normal file
70
code/static.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
// When writing a class, all of the properties and methods are being defined for the object
|
||||
// that will be created from the class.
|
||||
|
||||
class House
|
||||
{
|
||||
public $color;
|
||||
|
||||
public function __construct($color)
|
||||
{
|
||||
$this->color = $color;
|
||||
}
|
||||
}
|
||||
|
||||
// Like building a house, a class is a blueprint that
|
||||
// defines what the house can do and the object is the house itself that can actually
|
||||
// perform the actions defined in the blueprint.
|
||||
$house = new House('Green');
|
||||
|
||||
// However, what if you want the blueprint to have properties and methods?
|
||||
// That is when you use the "static" keyword. In this class, we will define a default color
|
||||
// on the class itself and then use it when creating a new object.
|
||||
class Skyscraper
|
||||
{
|
||||
private static $popularColor;
|
||||
public $color;
|
||||
|
||||
public static function setDefaultColor($color)
|
||||
{
|
||||
self::$popularColor = $color;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->color = self::$popularColor;
|
||||
}
|
||||
}
|
||||
|
||||
// You can access static methods and properties using double colons on "self" inside the object
|
||||
// or on the class name outside of the object. Static methods and properties can only access
|
||||
// other static methods and properties.
|
||||
Skyscraper::setDefaultColor('Grey');
|
||||
$skyscraper = new Skyscraper();
|
||||
echo $skyscraper->color . "\n";
|
||||
|
||||
// Often, you will see static constructors in PHP.
|
||||
// A static constructor creates a new instance of an object. Why would do that when you can just use "new Class" to create
|
||||
// the object? The most common reason is to make the code more readable.
|
||||
class TinyHouse
|
||||
{
|
||||
private $color;
|
||||
private $wheels;
|
||||
private $trailer;
|
||||
|
||||
public static function build($color, $wheels, $trailer)
|
||||
{
|
||||
return new self($color, $wheels, $trailer);
|
||||
}
|
||||
|
||||
public function __construct($color, $wheels, $trailer)
|
||||
{
|
||||
$this->color = $color;
|
||||
$this->wheels = $wheels;
|
||||
$this->trailer = $trailer;
|
||||
}
|
||||
}
|
||||
|
||||
// Using "build" can make more sense than "new", but it is ultimately a personal preference.
|
||||
$house = TinyHouse::build('Blue', 4, true);
|
23
code/strings.php
Normal file
23
code/strings.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
// As seen in the first chapter, a string is a group of characters created by
|
||||
// surrounding text in single or double quotes.
|
||||
$firstname = 'Joey';
|
||||
$lastname = "Johnson";
|
||||
|
||||
// A double quoted string can interpret special characters starting
|
||||
// with a back slash to create formatting. The \n creates a newline
|
||||
// between the names and after them.
|
||||
echo "Jacob\nJones\n";
|
||||
|
||||
// Double quoted strings can also embed variables in the text. This code
|
||||
// outputs "Cindy Smith".
|
||||
$firstname = 'Cindy';
|
||||
echo "$firstname Smith\n";
|
||||
|
||||
// Another feature of strings is the ability to combine them together.
|
||||
// To combine two strings, use the period character in between them.
|
||||
$firstname = 'Jenny';
|
||||
$lastname = 'Madison';
|
||||
$fullname = $firstname . $lastname;
|
||||
echo $fullname;
|
14
config.php
14
config.php
@@ -27,12 +27,18 @@ return [
|
||||
'title' => 'Arithmetic',
|
||||
'subtitle' => 'Doing math like a pro',
|
||||
'previous' => 'variables',
|
||||
'next' => 'strings',
|
||||
]),
|
||||
Page::create('strings', null, 'strings.php', [
|
||||
'title' => 'Strings',
|
||||
'subtitle' => 'Working with text',
|
||||
'previous' => 'arithmetic',
|
||||
'next' => 'comparisons',
|
||||
]),
|
||||
Page::create('comparisons', null, 'comparisons.php', [
|
||||
'title' => 'Comparisons',
|
||||
'subtitle' => 'Equality checking',
|
||||
'previous' => 'arithmetic',
|
||||
'previous' => 'strings',
|
||||
'next' => 'boolean-logic',
|
||||
]),
|
||||
Page::create('boolean-logic', null, 'boolean-logic.php', [
|
||||
@@ -87,6 +93,12 @@ return [
|
||||
'title' => 'Classes: Constructor',
|
||||
'subtitle' => 'Construct your objects',
|
||||
'previous' => 'classes-visibility',
|
||||
'next' => 'static',
|
||||
]),
|
||||
Page::create('static', null, 'static.php', [
|
||||
'title' => 'Static',
|
||||
'subtitle' => 'Adding properties and functions to the blueprint',
|
||||
'previous' => 'classes-constructor',
|
||||
'next' => '',
|
||||
]),
|
||||
],
|
||||
|
Reference in New Issue
Block a user