diff --git a/assets/templates/_table_of_contents.phtml b/assets/templates/_table_of_contents.phtml
index c6d9ad9..88c4ad3 100644
--- a/assets/templates/_table_of_contents.phtml
+++ b/assets/templates/_table_of_contents.phtml
@@ -5,16 +5,18 @@
Basics
Variables
Arithmetic
+ Strings
Comparisons
Boolean Logic
Conditionals
Loops
Arrays
Functions
- Classes: Introduction
+ Classes
Classes: Inheritance
Classes: Visibility
Classes: Constructor
+ Static
Credits
diff --git a/code/basics.php b/code/basics.php
index 81caa89..e62abbd 100644
--- a/code/basics.php
+++ b/code/basics.php
@@ -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";
diff --git a/code/classes-inheritance.php b/code/classes-inheritance.php
index f1eed47..2bbb2cd 100644
--- a/code/classes-inheritance.php
+++ b/code/classes-inheritance.php
@@ -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();
diff --git a/code/classes-visibility.php b/code/classes-visibility.php
index 2833608..509e89b 100644
--- a/code/classes-visibility.php
+++ b/code/classes-visibility.php
@@ -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
{
diff --git a/code/classes.php b/code/classes.php
index c3e6a19..039d816 100644
--- a/code/classes.php
+++ b/code/classes.php
@@ -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();
diff --git a/code/static.php b/code/static.php
new file mode 100644
index 0000000..ceba59f
--- /dev/null
+++ b/code/static.php
@@ -0,0 +1,70 @@
+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);
diff --git a/code/strings.php b/code/strings.php
new file mode 100644
index 0000000..677b503
--- /dev/null
+++ b/code/strings.php
@@ -0,0 +1,23 @@
+ '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' => '',
]),
],