From b1fb56c1c258883f2cef989da8916b93d80ee78e Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 5 Sep 2018 20:18:32 -0500 Subject: [PATCH 1/4] Added static page --- assets/templates/_table_of_contents.phtml | 1 + code/static.php | 70 +++++++++++++++++++++++ config.php | 6 ++ 3 files changed, 77 insertions(+) create mode 100644 code/static.php diff --git a/assets/templates/_table_of_contents.phtml b/assets/templates/_table_of_contents.phtml index c6d9ad9..a0e2e0c 100644 --- a/assets/templates/_table_of_contents.phtml +++ b/assets/templates/_table_of_contents.phtml @@ -15,6 +15,7 @@
  • Classes: Inheritance
  • Classes: Visibility
  • Classes: Constructor
  • +
  • Static
  • Credits diff --git a/code/static.php b/code/static.php new file mode 100644 index 0000000..1050bcc --- /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 just use "new Class" to create +// a new 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 choice. +$house = TinyHouse::build('Blue', 4, true); diff --git a/config.php b/config.php index e584669..f4359eb 100644 --- a/config.php +++ b/config.php @@ -87,6 +87,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' => '', ]), ], From 9cf9559aea52fd437d2a2b9dfbcc4877281c1f8c Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 5 Sep 2018 20:57:50 -0500 Subject: [PATCH 2/4] Added strings page --- assets/templates/_table_of_contents.phtml | 3 ++- code/strings.php | 23 +++++++++++++++++++++++ config.php | 8 +++++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 code/strings.php diff --git a/assets/templates/_table_of_contents.phtml b/assets/templates/_table_of_contents.phtml index a0e2e0c..88c4ad3 100644 --- a/assets/templates/_table_of_contents.phtml +++ b/assets/templates/_table_of_contents.phtml @@ -5,13 +5,14 @@
  • Basics
  • Variables
  • Arithmetic
  • +
  • Strings
  • Comparisons
  • Boolean Logic
  • Conditionals
  • Loops
  • Arrays
  • Functions
  • -
  • Classes: Introduction
  • +
  • Classes
  • Classes: Inheritance
  • Classes: Visibility
  • Classes: Constructor
  • 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', [ From b9f18acc1f6dad16193df4aaad5f46c464708747 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 5 Sep 2018 20:58:01 -0500 Subject: [PATCH 3/4] Tweaked basics page --- code/basics.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) 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"; From 9c86b5d6087471d111fae701ea9e5120584f55ad Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 5 Sep 2018 20:58:12 -0500 Subject: [PATCH 4/4] Proofreading classes pages --- code/classes-inheritance.php | 2 +- code/classes-visibility.php | 4 ++-- code/classes.php | 1 + code/static.php | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) 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 index 1050bcc..ceba59f 100644 --- a/code/static.php +++ b/code/static.php @@ -45,8 +45,8 @@ $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 just use "new Class" to create -// a new object? The most common reason is to make the code more readable. +// 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; @@ -66,5 +66,5 @@ class TinyHouse } } -// Using "build" can make more sense than "new", but it is ultimately a personal choice. +// Using "build" can make more sense than "new", but it is ultimately a personal preference. $house = TinyHouse::build('Blue', 4, true);