mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-07-10 01:56:20 +02:00
Converted chapters to markdown and changed pages to vertical layout
This commit is contained in:
@ -13,8 +13,8 @@ body {
|
||||
padding-bottom: 1em;
|
||||
}
|
||||
pre, code {
|
||||
font-family: Consolas, monaco, monospace;
|
||||
font-size: 16px;
|
||||
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
|
||||
font-size: 14px;
|
||||
margin: 0 !important;
|
||||
border-radius: 0 !important;
|
||||
padding-top: 0.5em !important;
|
||||
@ -32,10 +32,12 @@ hr {
|
||||
border: 0;
|
||||
height: 1px;
|
||||
background-color: #EEE;
|
||||
margin: 0.5em 0;
|
||||
}
|
||||
p {
|
||||
max-width: 40em;
|
||||
line-height: 1.5;
|
||||
margin-top: 1.5em;
|
||||
margin-bottom: 0.75em;
|
||||
}
|
||||
ol {
|
||||
padding-left: 1em;
|
||||
@ -99,9 +101,10 @@ button:hover .icon svg {
|
||||
clear: both;
|
||||
}
|
||||
.container {
|
||||
width: 1050px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
max-width: 1050px;
|
||||
}
|
||||
.container.small {
|
||||
max-width: 750px;
|
||||
}
|
||||
.center {
|
||||
margin-left: auto;
|
||||
@ -159,8 +162,8 @@ button:hover .icon svg {
|
||||
.navigate-links a:hover {
|
||||
text-decoration: none;
|
||||
}
|
||||
.table-of-contents {
|
||||
padding: 2em;
|
||||
.table-of-contents h4 {
|
||||
margin-top: 0;
|
||||
}
|
||||
.table-of-contents ol {
|
||||
margin: 1em 0;
|
||||
@ -174,22 +177,21 @@ button:hover .icon svg {
|
||||
width: 1em;
|
||||
display: inline-block;
|
||||
}
|
||||
.home-title-wrapper {
|
||||
display: grid;
|
||||
grid-template-columns: 175px 1fr;
|
||||
grid-template-rows: 1fr 1fr;
|
||||
}
|
||||
.home-title {
|
||||
|
||||
text-align: center;
|
||||
}
|
||||
.home-subtitle {
|
||||
text-align: center;
|
||||
margin-top: 0;
|
||||
grid-column-start: 2;
|
||||
grid-column-end: 3;
|
||||
}
|
||||
.home-logo {
|
||||
grid-row-start: 1;
|
||||
grid-row-end: 3;
|
||||
display: block;
|
||||
margin: 0 auto;
|
||||
width: 150px;
|
||||
height: 100px;
|
||||
margin-bottom: 0.5em;
|
||||
}
|
||||
.home-logo svg {
|
||||
width: 150px;
|
||||
@ -221,6 +223,9 @@ button:hover .icon svg {
|
||||
box-shadow: 0 0 10px 0 $drop-shadow;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.modal-content .table-of-contents {
|
||||
padding: 2em;
|
||||
}
|
||||
.closed {
|
||||
display: none;
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
|
||||
<title><?= $title ?? 'PHP Apprentice' ?></title>
|
||||
<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="<?php echo asset('css/site.css') ?>">
|
||||
<link rel="icon" href="/favicon-32.png">
|
||||
|
@ -1,40 +1,51 @@
|
||||
<?php partial('header', ['title' => 'PHP Apprentice - Credits']) ?>
|
||||
|
||||
<div class="container center">
|
||||
<div class="grid-toc">
|
||||
<div>
|
||||
<h1>Credits</h1>
|
||||
<p>
|
||||
PHP Apprentice was inspired by
|
||||
<a href="https://gobyexample.com/">Go By Example</a> and by <a href="https://elixirschool.com/">Elixir School</a>. Both sites offer excellent, quality documentation in Go and Elixir and I want PHP Apprentice to provide the same
|
||||
experience for the PHP programming language.
|
||||
</p>
|
||||
<p>
|
||||
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>
|
||||
</ul>
|
||||
</p>
|
||||
<p>
|
||||
PHP Apprentice was built using several open source projects, besides PHP itself.
|
||||
Thank you to each of these maintainers for providing great libraries!
|
||||
<ul>
|
||||
<li><a href="https://prismjs.com/" target="_blank">Prism.js</a></li>
|
||||
<li><a href="https://www.zondicons.com/" target="_blank">Zondicons by Steve Schoger</a></li>
|
||||
<li><a href="https://postcss.org/" target="_blank">PostCSS</a></li>
|
||||
<li><a href="https://symfony.com/doc/current/components/console.html" target="_blank">Symfony CLI</a></li>
|
||||
<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') ?>
|
||||
</div>
|
||||
<div class="container small center">
|
||||
<div class="menu">
|
||||
<button class="menu-button" title="Open Menu">
|
||||
<div class="icon"><?= icon('menu') ?></div>
|
||||
Menu
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h1>Credits</h1>
|
||||
<p>
|
||||
PHP Apprentice was inspired by
|
||||
<a href="https://gobyexample.com/">Go By Example</a> and by <a href="https://elixirschool.com/">Elixir School</a>. Both sites offer excellent, quality documentation in Go and Elixir and I want PHP Apprentice to provide the same
|
||||
experience for the PHP programming language.
|
||||
</p>
|
||||
<p>
|
||||
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>
|
||||
</ul>
|
||||
</p>
|
||||
<p>
|
||||
PHP Apprentice was built using several open source projects, besides PHP itself.
|
||||
Thank you to each of these maintainers for providing great libraries!
|
||||
<ul>
|
||||
<li><a href="https://prismjs.com/" target="_blank">Prism.js</a></li>
|
||||
<li><a href="https://www.zondicons.com/" target="_blank">Zondicons by Steve Schoger</a></li>
|
||||
<li><a href="https://postcss.org/" target="_blank">PostCSS</a></li>
|
||||
<li><a href="https://symfony.com/doc/current/components/console.html" target="_blank">Symfony CLI</a></li>
|
||||
<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>
|
||||
|
||||
<div class="modal closed">
|
||||
<div class="modal-content">
|
||||
<button class="modal-button right" title="Close">
|
||||
<div class="icon"><?= icon('close-outline') ?></div>
|
||||
</button>
|
||||
<?php partial('table_of_contents') ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php partial('header', ['title' => $title, 'subtitle' => $subtitle]) ?>
|
||||
|
||||
<div class="container center">
|
||||
<div class="container small center">
|
||||
<div class="menu">
|
||||
<button class="menu-button" title="Open Menu">
|
||||
<div class="icon"><?= icon('menu') ?></div>
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
<h1><?= escape($title) ?></h1>
|
||||
<h3 class="subtitle"><?= escape($subtitle) ?></h3>
|
||||
<?= code_table($code) ?>
|
||||
<?= $chapter ?>
|
||||
|
||||
<div class="clearfix"></div>
|
||||
<div class="navigate-links">
|
||||
|
@ -1,35 +1,45 @@
|
||||
<?php partial('header', ['title' => 'PHP Apprentice']) ?>
|
||||
|
||||
<div class="container center">
|
||||
<div class="grid-toc">
|
||||
<div>
|
||||
<div class="home-title-wrapper">
|
||||
<div class="home-logo"><?= icon('elephant') ?></div>
|
||||
<h1 class="home-title">PHP Apprentice</h1>
|
||||
<h3 class="home-subtitle">A site for learning how to use PHP</h3>
|
||||
</div>
|
||||
<p>
|
||||
The goal of PHP Apprentice is to be an easy to understand resource for learning how to write good code in the PHP programming language. There are a lot of PHP tutorials on the internet that use outdated libraries, insecure programming practices or inefficient code. I want this site to show how to write PHP code with quality.
|
||||
</p>
|
||||
<p>
|
||||
The site currently has content for learning the basics of PHP. In the future, more pages will be added for more advanced topics like building websites, database integration and security.
|
||||
</p>
|
||||
<p>
|
||||
PHP Apprentice is currently a work in progress. If you would like to contribute or request a certain discussion topic, check out the <a href="https://github.com/restoreddev/phpapprentice" target="_blank">GitHub repository</a>.
|
||||
</p>
|
||||
<p>
|
||||
To get started, you will need to <a href="<?php echo page_path('installing-php') ?>">install</a> PHP, have a text editor and open your terminal.
|
||||
Each example in PHP Apprentice can by typed into a PHP file and executed in the terminal.
|
||||
Let's get started! 😃
|
||||
</p>
|
||||
<a href="<?= page_path('basics') ?>" class="button">
|
||||
<div class="icon"><?= icon('book-reference') ?></div>
|
||||
Open First Chapter
|
||||
</a>
|
||||
</div>
|
||||
<div>
|
||||
<?php partial('table_of_contents') ?>
|
||||
<div class="container small center">
|
||||
<div class="menu">
|
||||
<button class="menu-button" title="Open Menu">
|
||||
<div class="icon"><?= icon('menu') ?></div>
|
||||
Menu
|
||||
</button>
|
||||
</div>
|
||||
<div>
|
||||
<div class="home-title-wrapper">
|
||||
<div class="home-logo"><?= icon('elephant') ?></div>
|
||||
<h1 class="home-title">PHP Apprentice</h1>
|
||||
<h3 class="home-subtitle">A site for learning how to use PHP</h3>
|
||||
</div>
|
||||
<p>
|
||||
The goal of PHP Apprentice is to be an easy to understand resource for learning how to write good code in the PHP programming language. There are a lot of PHP tutorials on the internet that use outdated libraries, insecure programming practices or inefficient code. I want this site to show how to write PHP code with quality.
|
||||
</p>
|
||||
<p>
|
||||
The site currently has content for learning the basics of PHP. In the future, more pages will be added for more advanced topics like building websites, database integration and security.
|
||||
</p>
|
||||
<p>
|
||||
PHP Apprentice is currently a work in progress. If you would like to contribute or request a certain discussion topic, check out the <a href="https://github.com/restoreddev/phpapprentice" target="_blank">GitHub repository</a>.
|
||||
</p>
|
||||
<p>
|
||||
To get started, you will need to <a href="<?php echo page_path('installing-php') ?>">install</a> PHP, have a text editor and open your terminal.
|
||||
Each example in PHP Apprentice can by typed into a PHP file and executed in the terminal.
|
||||
Let's get started! 😃
|
||||
</p>
|
||||
<a href="<?= page_path('basics') ?>" class="button">
|
||||
<div class="icon"><?= icon('book-reference') ?></div>
|
||||
Open First Chapter
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal closed">
|
||||
<div class="modal-content">
|
||||
<button class="modal-button right" title="Close">
|
||||
<div class="icon"><?= icon('close-outline') ?></div>
|
||||
</button>
|
||||
<?php partial('table_of_contents') ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -1,30 +1,41 @@
|
||||
<?php partial('header', ['title' => 'PHP Apprentice - Installing PHP']) ?>
|
||||
|
||||
<div class="container center">
|
||||
<div class="grid-toc">
|
||||
<div>
|
||||
<h1>Installing PHP</h1>
|
||||
<h2>Windows 10</h2>
|
||||
<ol class="body-ol">
|
||||
<li>Download PHP from the <a href="https://windows.php.net/download">PHP For Windows</a> site. I recommend downloading the non-thread safe PHP 7.1 or 7.2 (unless you want to use Apache and you know if you do). You will also need to choose 32 bit (x86) or 64 bit (x64) depending on your version of Windows.</li>
|
||||
<li>Download the corresponding Visual C++ Redistributable from the same site. For PHP 7.1, it is VC14 and for PHP 7.2, it is VC15.</li>
|
||||
<li>Install the VC redistrubutable using the downloaded executable.</li>
|
||||
<li>The PHP download comes in a zip folder. Extract the zip folder into your user directory in a folder named "php". The path should look like "C:\Users\username\php".</li>
|
||||
<li>Next, we need to add PHP to your environment variables path. Open "System" under "Control Panel". Go to the "Advanced" tab and select "Environment Variables". In the "User variables" section, select "Path" and click the "Edit" button. You will see a list of different folder paths. Add the PHP path to the list using the "Add" button. The PHP path is the same folder where you extracted PHP.</li>
|
||||
<li>Now, you can open PowerShell or Command Prompt and type "php -v" to verify PHP was installed correctly. It will return the installed version of PHP on your system.</li>
|
||||
</ol>
|
||||
<div class="container small center">
|
||||
<div class="menu">
|
||||
<button class="menu-button" title="Open Menu">
|
||||
<div class="icon"><?= icon('menu') ?></div>
|
||||
Menu
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<h2>MacOS</h2>
|
||||
<p>Good news! Each version of MacOS comes with PHP by default. However, if you are running an older version of MacOS or OS X, then you will need to manually install a new version of PHP. To check your current version, open Terminal and type "php -v". You will need at least PHP 7.1 on your computer to use all the features in these tutorials.</p>
|
||||
<ol class="body-ol">
|
||||
<li>Install <a href="https://brew.sh/">Homebrew</a>.</li>
|
||||
<li>Run "brew install php" in Terminal.</li>
|
||||
<li>Check your version is correct by running "php -v" in Terminal.</li>
|
||||
</ol>
|
||||
</div>
|
||||
<div>
|
||||
<?php partial('table_of_contents') ?>
|
||||
</div>
|
||||
<div>
|
||||
<h1>Installing PHP</h1>
|
||||
<h2>Windows 10</h2>
|
||||
<ol class="body-ol">
|
||||
<li>Download PHP from the <a href="https://windows.php.net/download">PHP For Windows</a> site. I recommend downloading the non-thread safe PHP 7.1 or 7.2 (unless you want to use Apache and you know if you do). You will also need to choose 32 bit (x86) or 64 bit (x64) depending on your version of Windows.</li>
|
||||
<li>Download the corresponding Visual C++ Redistributable from the same site. For PHP 7.1, it is VC14 and for PHP 7.2, it is VC15.</li>
|
||||
<li>Install the VC redistrubutable using the downloaded executable.</li>
|
||||
<li>The PHP download comes in a zip folder. Extract the zip folder into your user directory in a folder named "php". The path should look like "C:\Users\username\php".</li>
|
||||
<li>Next, we need to add PHP to your environment variables path. Open "System" under "Control Panel". Go to the "Advanced" tab and select "Environment Variables". In the "User variables" section, select "Path" and click the "Edit" button. You will see a list of different folder paths. Add the PHP path to the list using the "Add" button. The PHP path is the same folder where you extracted PHP.</li>
|
||||
<li>Now, you can open PowerShell or Command Prompt and type "php -v" to verify PHP was installed correctly. It will return the installed version of PHP on your system.</li>
|
||||
</ol>
|
||||
|
||||
<h2>MacOS</h2>
|
||||
<p>Good news! Each version of MacOS comes with PHP by default. However, if you are running an older version of MacOS or OS X, then you will need to manually install a new version of PHP. To check your current version, open Terminal and type "php -v". You will need at least PHP 7.1 on your computer to use all the features in these tutorials.</p>
|
||||
<ol class="body-ol">
|
||||
<li>Install <a href="https://brew.sh/">Homebrew</a>.</li>
|
||||
<li>Run "brew install php" in Terminal.</li>
|
||||
<li>Check your version is correct by running "php -v" in Terminal.</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal closed">
|
||||
<div class="modal-content">
|
||||
<button class="modal-button right" title="Close">
|
||||
<div class="icon"><?= icon('close-outline') ?></div>
|
||||
</button>
|
||||
<?php partial('table_of_contents') ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
58
chapters/abstract.md
Normal file
58
chapters/abstract.md
Normal file
@ -0,0 +1,58 @@
|
||||
Abstract classes are similar to interfaces in that they define methods that a sub-class must implement.
|
||||
However, an abstract class can also have normal methods. To create an abstract class, use the "abstract"
|
||||
keyword followed by class and the name of the class.
|
||||
```php
|
||||
<?php
|
||||
|
||||
abstract class CellPhone
|
||||
{
|
||||
abstract public function unlock();
|
||||
|
||||
public function turnOn()
|
||||
{
|
||||
echo "Holding power button...\n";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
To use an abstract class, you create another class that extends it and create any methods that were marked as abstract.
|
||||
A class can only extend one abstract class and the child class has to implement all abstract methods.
|
||||
```php
|
||||
class iPhone extends CellPhone
|
||||
{
|
||||
public function unlock()
|
||||
{
|
||||
echo "Touching fingerprint reader...\n";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this example, we use an abstract class to create the behavior for turning on a cell phone and then
|
||||
force the child classes to implement how to unlock the phone. We have clearly defined what a cell phone
|
||||
performs and we have limited code duplication.
|
||||
```php
|
||||
class Android extends CellPhone
|
||||
{
|
||||
public function unlock()
|
||||
{
|
||||
echo "Typing passcode...\n";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Our iPhone and Android classes can now both use the turnOn method and the unlock method.
|
||||
```php
|
||||
$iPhone = new iPhone();
|
||||
$iPhone->turnOn();
|
||||
$iPhone->unlock();
|
||||
|
||||
$android = new Android();
|
||||
$android->turnOn();
|
||||
$android->unlock();
|
||||
```
|
||||
|
||||
Lastly, you cannot create an instance of an abstract class. PHP would not know how to use the abstract methods
|
||||
so when you try to create an abstract instance you will get an error.
|
||||
```php
|
||||
$cellPhone = new CellPhone();
|
||||
```
|
40
chapters/arithmetic.md
Normal file
40
chapters/arithmetic.md
Normal file
@ -0,0 +1,40 @@
|
||||
Now that we know how to create variables, let's look at doing some math.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
```
|
||||
|
||||
To add two values, use the plus symbol.
|
||||
```php
|
||||
echo $a + $b;
|
||||
```
|
||||
|
||||
To subtract, use the minus symbol.
|
||||
```php
|
||||
echo $b - $a;
|
||||
```
|
||||
|
||||
To multiply two values, use an asterisk.
|
||||
```php
|
||||
echo $a * $b;
|
||||
```
|
||||
|
||||
To divide values, use a forward slash.
|
||||
```php
|
||||
echo $b / $a;
|
||||
```
|
||||
|
||||
PHP uses the percent symbol for calculating the modulus of two numbers.
|
||||
The modulus is calculated by dividing two numbers and returning the remainder of the result.
|
||||
In this example, the value of $mod will be 0.
|
||||
```php
|
||||
$mod = 10 % 5;
|
||||
```
|
||||
|
||||
You can also use double asterisks to calculate a number to the power of another number.
|
||||
The following statement will print 25.
|
||||
```php
|
||||
echo 5 ** 2;
|
||||
```
|
38
chapters/arrays.md
Normal file
38
chapters/arrays.md
Normal file
@ -0,0 +1,38 @@
|
||||
In PHP, arrays are used to store a list of items in a single variable.
|
||||
There are two ways to create an array.
|
||||
|
||||
First, you can use the array construct to pass in values separated by commas
|
||||
and it will return an array.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$taskList = array('grocery store', 'change car oil');
|
||||
```
|
||||
|
||||
Second, you can surround the list in square brackets.
|
||||
This style is the most common and recommended form
|
||||
of creating an array.
|
||||
```php
|
||||
$groceryList = ['bread', 'milk', 'eggs'];
|
||||
```
|
||||
|
||||
PHP will automatically assign index keys for each value in an array
|
||||
starting with 0. So, to access a value in an array you will
|
||||
pass the key number into brackets after the variable name.
|
||||
```php
|
||||
echo $groceryList[0] . "\n";
|
||||
echo $groceryList[1] . "\n";
|
||||
```
|
||||
|
||||
You can also assign keys in an array using numbers or strings.
|
||||
It is very common to create an array with string keys. The pattern
|
||||
is called an associative array or a map.
|
||||
```php
|
||||
$car = ['make' => 'Toyota', 'model' => 'Camry'];
|
||||
```
|
||||
|
||||
To access the value in an associative array, just use the string key in brackets
|
||||
after the variable name.
|
||||
```php
|
||||
echo $car['model'] . "\n";
|
||||
```
|
33
chapters/basics.md
Normal file
33
chapters/basics.md
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
In the tradition of our ancestors, let's start with a hello world program.
|
||||
All PHP files must start with a <?php tag unless it is for a html template.
|
||||
(We will learn about html templates later.)
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
echo "Hello World!\n";
|
||||
```
|
||||
|
||||
There is a lot going on in the above code so let's work through it.
|
||||
|
||||
First, the echo keyword tells PHP to output 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.
|
||||
Double quoted strings can hold special characters like \n which tells PHP to start a new line.
|
||||
```php
|
||||
"\nI am a string on a new line";
|
||||
```
|
||||
|
||||
Third, all lines of code in PHP must end in a semi-colon.
|
||||
```php
|
||||
echo "No semi-colon is a no-no\n";
|
||||
```
|
||||
|
||||
Using semi-colons means we can write multiple statements on one line.
|
||||
```php
|
||||
echo "Hello"; echo " World\n";
|
||||
```
|
53
chapters/boolean-logic.md
Normal file
53
chapters/boolean-logic.md
Normal file
@ -0,0 +1,53 @@
|
||||
Boolean logic is used to combine booleans to return another boolean.
|
||||
|
||||
Using double ampersands tells PHP to check if both values are true.
|
||||
If so, it will return true. If not, it will return false.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$a = true;
|
||||
$b = true;
|
||||
$c = false;
|
||||
```
|
||||
|
||||
Returns true.
|
||||
```php
|
||||
$a && $b;
|
||||
```
|
||||
Returns false.
|
||||
```php
|
||||
$a && $c;
|
||||
```
|
||||
|
||||
Using two pipe characters checks if either value is true.
|
||||
Then, it will return true. If both values are false, then PHP
|
||||
returns false.
|
||||
```php
|
||||
$a = true;
|
||||
$b = false;
|
||||
$c = false;
|
||||
$d = true;
|
||||
```
|
||||
|
||||
Returns true.
|
||||
```php
|
||||
$a || $b;
|
||||
```
|
||||
Returns false.
|
||||
```php
|
||||
$b || $c;
|
||||
```
|
||||
Returns true.
|
||||
```php
|
||||
$a || $d;
|
||||
```
|
||||
|
||||
Using an exclamation point returns the value flipped.
|
||||
```php
|
||||
$d = true;
|
||||
```
|
||||
|
||||
Outputs false.
|
||||
```php
|
||||
echo !$d;
|
||||
```
|
59
chapters/classes-constructor.md
Normal file
59
chapters/classes-constructor.md
Normal file
@ -0,0 +1,59 @@
|
||||
Whenever you create an object in PHP, you put parentheses after the class name.
|
||||
In the previous examples, we always left the parentheses empty.
|
||||
```php
|
||||
<?php
|
||||
|
||||
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.
|
||||
```php
|
||||
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:
|
||||
```php
|
||||
$hat = new Hat();
|
||||
$hat->setColor('Red');
|
||||
```
|
||||
|
||||
You can write:
|
||||
```php
|
||||
$ballcap = new Ballcap('Blue');
|
||||
```
|
||||
|
||||
Constructors do not return values because the return value is a always a new object.
|
||||
```php
|
||||
class Tophat
|
||||
{
|
||||
public function __construct($color)
|
||||
{
|
||||
return $color;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
"$tophat" now holds an instance of Tophat, not the color "Grey".
|
||||
```php
|
||||
$tophat = new Tophat('Grey');
|
||||
```
|
78
chapters/classes-inheritance.md
Normal file
78
chapters/classes-inheritance.md
Normal file
@ -0,0 +1,78 @@
|
||||
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.
|
||||
```php
|
||||
<?php
|
||||
|
||||
class Vehicle
|
||||
{
|
||||
public function drive()
|
||||
{
|
||||
echo "driving...\n";
|
||||
}
|
||||
}
|
||||
|
||||
class Truck extends Vehicle {}
|
||||
```
|
||||
|
||||
The `Truck` class does not error because `Truck` extends `Vehicle`.
|
||||
```php
|
||||
$truck = new Truck();
|
||||
$truck->drive();
|
||||
```
|
||||
|
||||
Even though the child class inherits a parent class' properties and methods,
|
||||
the child can still override the parent.
|
||||
```php
|
||||
class Tractor extends Vehicle
|
||||
{
|
||||
public function drive()
|
||||
{
|
||||
echo "driving slowly...\n";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The drive function now outputs "driving slowly..." instead of "driving...".
|
||||
```php
|
||||
$tractor = new Tractor();
|
||||
$tractor->drive();
|
||||
```
|
||||
|
||||
A class can use a parent's property or method from the "$this" variable.
|
||||
```php
|
||||
class Motorcycle extends Vehicle
|
||||
{
|
||||
public function pushPedal()
|
||||
{
|
||||
$this->drive();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Outputs "driving...".
|
||||
```php
|
||||
$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.
|
||||
```php
|
||||
class Racecar extends Vehicle
|
||||
{
|
||||
public function drive()
|
||||
{
|
||||
parent::drive();
|
||||
|
||||
echo "driving even faster...\n";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Outputs "driving..." and "driving even faster...".
|
||||
```php
|
||||
$racecar = new Racecar();
|
||||
$racecar->drive();
|
||||
```
|
70
chapters/classes-visibility.md
Normal file
70
chapters/classes-visibility.md
Normal file
@ -0,0 +1,70 @@
|
||||
In the last chapter, we defined properties and methods on the class using the public keyword.
|
||||
You can also define them using the "protected" and "private" keywords.
|
||||
Both keywords prevent the properties and functions from being accessible outside the object.
|
||||
Only the object itself can use each.
|
||||
```php
|
||||
<?php
|
||||
|
||||
class Phone
|
||||
{
|
||||
private $number;
|
||||
|
||||
public function setNumber($number)
|
||||
{
|
||||
$this->number = $number;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
We cannot set the number using "$phone->number = '123-456-7890'".
|
||||
Instead, we can use the public method.
|
||||
```php
|
||||
$phone = new Phone();
|
||||
$phone->setNumber('123-456-7890');
|
||||
```
|
||||
|
||||
Making an attribute or function private, gives you more control over the data in the object.
|
||||
For example, we could prevent a number being set if it starts with a 7.
|
||||
```php
|
||||
class Phone2
|
||||
{
|
||||
private $number;
|
||||
|
||||
public function setNumber($number)
|
||||
{
|
||||
if (substr($number, 0, 1) !== '7') {
|
||||
$this->number = $number;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
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 class.
|
||||
```php
|
||||
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 "number" property
|
||||
because it is still listed as private.
|
||||
```php
|
||||
class Smartphone extends Phone3
|
||||
{
|
||||
public function setCaller($caller)
|
||||
{
|
||||
$this->caller = $caller;
|
||||
}
|
||||
}
|
||||
```
|
64
chapters/classes.md
Normal file
64
chapters/classes.md
Normal file
@ -0,0 +1,64 @@
|
||||
|
||||
Classes allow you to define your own data types. All classes start with the
|
||||
class keyword followed by the name of the class and opening and closing curly braces.
|
||||
```php
|
||||
<?php
|
||||
|
||||
class Car
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
To create an instance of a class, you use the "new" keyword in front of the class name
|
||||
with parentheses.
|
||||
```php
|
||||
$car = new Car();
|
||||
```
|
||||
|
||||
A class can define attributes and methods. An attribute is a piece of data
|
||||
stored on the class instance. You can define an attribute by adding the
|
||||
word "public" and a variable name inside the class definition.
|
||||
```php
|
||||
class Bicycle
|
||||
{
|
||||
public $color;
|
||||
}
|
||||
```
|
||||
|
||||
Then, when you create an instance of the class, you can set and use
|
||||
the color attribute on the bicycle using "->".
|
||||
```php
|
||||
$bike = new Bicycle();
|
||||
$bike->color = 'Blue';
|
||||
echo $bike->color . "\n";
|
||||
```
|
||||
|
||||
An instance of a class is called an object. Congratulations!
|
||||
You are now performing object-oriented development.
|
||||
```php
|
||||
$redBike = new Bicycle();
|
||||
$redBike->color = 'Red';
|
||||
echo $redBike->color . " Bike Object\n";
|
||||
```
|
||||
|
||||
A method is a function attached to the class. You can add a method
|
||||
to a class by using the "public" keyword followed by the function. A method
|
||||
can access the attributes and methods of an object instance using the "$this" variable.
|
||||
```php
|
||||
class Tricycle
|
||||
{
|
||||
public $color;
|
||||
|
||||
public function echoColor()
|
||||
{
|
||||
echo $this->color . "\n";
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can execute a method on an object using the same "->" arrow characters.
|
||||
```php
|
||||
$bike = new Tricycle();
|
||||
$bike->color = 'Red';
|
||||
$bike->echoColor();
|
||||
```
|
65
chapters/comparisons.md
Normal file
65
chapters/comparisons.md
Normal file
@ -0,0 +1,65 @@
|
||||
|
||||
A boolean is a value that is always 0 or 1, yes or no, on or off.
|
||||
In PHP, a boolean is represented by the words true and false.
|
||||
While programming, you will often want to know if something is positive or negative.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$a = true;
|
||||
$b = false;
|
||||
```
|
||||
|
||||
There are many constructs and functions that will return a boolean.
|
||||
To start, let's look at comparisons.
|
||||
```php
|
||||
$one = 1;
|
||||
$two = 2;
|
||||
```
|
||||
|
||||
Double equals checks if two values are equal.
|
||||
This statement will return false.
|
||||
```php
|
||||
$one == $two;
|
||||
```
|
||||
|
||||
An exclamation point and equal sign check if two values are not equal.
|
||||
This statement will return true.
|
||||
```php
|
||||
$one != $two;
|
||||
```
|
||||
|
||||
You can use greater than and less than symbols to check for comparisons too.
|
||||
This statement will return false.
|
||||
```php
|
||||
$one > $two;
|
||||
```
|
||||
|
||||
This statement will return true.
|
||||
```php
|
||||
$one < $two;
|
||||
```
|
||||
|
||||
If you combine a greater than or less than symbol with an equal,
|
||||
it will check if the value is greater or less than or equal to another value.
|
||||
```php
|
||||
$one <= $two;
|
||||
$one >= $two;
|
||||
```
|
||||
You can also check that two values are equal and of the same type
|
||||
by using three equal signs.
|
||||
|
||||
This returns true.
|
||||
```php
|
||||
1 == 1;
|
||||
1 == '1';
|
||||
1 == true;
|
||||
1 == 1.0;
|
||||
1 === 1;
|
||||
```
|
||||
|
||||
This returns false.
|
||||
```php
|
||||
1 === '1';
|
||||
1 === true;
|
||||
1 === 1.0;
|
||||
```
|
84
chapters/conditionals.md
Normal file
84
chapters/conditionals.md
Normal file
@ -0,0 +1,84 @@
|
||||
|
||||
When writing code, there will be times when you need to perform actions only under certain circumstances.
|
||||
There are several ways to control execution in PHP.
|
||||
We will start with an if statement.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$animal = 'cow';
|
||||
if ($animal == 'cow') {
|
||||
echo "Moooooo.....\n";
|
||||
}
|
||||
```
|
||||
|
||||
All conditionals check to see if a statement evaluates to true or false.
|
||||
In the case above, since $animal equals 'cow', the statement returns true and the contents of the if statement are executed.
|
||||
|
||||
An if statement can have multiple conditions chained together.
|
||||
If the first if statement returns false, then PHP will check each elseif.
|
||||
If none of the checks return true, then the else block will be executed.
|
||||
```php
|
||||
$animal = 'bird';
|
||||
if ($animal == 'dog') {
|
||||
echo "Woof! 🐶\n";
|
||||
} elseif ($animal == 'cat') {
|
||||
echo "Meow!? 🐱\n";
|
||||
} elseif ($animal == 'bird') {
|
||||
echo "Chirp! 🐦\n";
|
||||
} else {
|
||||
echo "I am not a dog, cat or bird\n";
|
||||
}
|
||||
```
|
||||
|
||||
An alternative to the if statement is the switch.
|
||||
A switch statement has multiple cases to check if the value in parentheses equals something.
|
||||
In this statement, since $food equals 'apples', the switch will echo "Eating an apple".
|
||||
The default case will be run if no other case evaluates to true, like an else statement.
|
||||
```php
|
||||
$food = 'apples';
|
||||
switch ($food) {
|
||||
case 'apples':
|
||||
echo "Eating an apple\n";
|
||||
break;
|
||||
case 'oranges':
|
||||
echo "Eating an orange\n";
|
||||
break;
|
||||
case 'peaches':
|
||||
echo "Eating a peach\n";
|
||||
break;
|
||||
default:
|
||||
echo "No food, I am hungry\n";
|
||||
}
|
||||
```
|
||||
|
||||
Breaks are a special keyword that tell PHP to stop execution once a case passes.
|
||||
If you do not use a break, PHP will continue to execute all following cases.
|
||||
In this switch, both "Drinking water" and "Drinking tea" will be executed since there is no break in the 'water' case.
|
||||
```php
|
||||
$drink = 'water';
|
||||
switch ($drink) {
|
||||
case 'water':
|
||||
echo "Drinking water\n";
|
||||
case 'tea':
|
||||
echo "Drinking tea\n";
|
||||
break;
|
||||
}
|
||||
```
|
||||
|
||||
PHP also supports single line conditionals called a ternary.
|
||||
In a ternary, the condition is followed by a question mark before the value that should be returned if the condition is true and then another colon and a value to return if the condition is false.
|
||||
```php
|
||||
$language = 'english';
|
||||
echo $language == 'spanish' ? "hola\n" : "hello\n";
|
||||
```
|
||||
|
||||
Lastly, there is another form of a ternary that checks if a value is set and then returns the value to the right of the two question marks if the value is null.
|
||||
```php
|
||||
echo $IDoNotExist ?? "Variable not set\n";
|
||||
```
|
||||
|
||||
You can also chain multiple checks in a row.
|
||||
```php
|
||||
$IExist = "Variable exists\n";
|
||||
echo $IDoNotExist ?? $IExist ?? "Neither variable is set\n";
|
||||
```
|
74
chapters/exceptions.md
Normal file
74
chapters/exceptions.md
Normal file
@ -0,0 +1,74 @@
|
||||
Sometimes things go wrong when someone uses your code. How do we handle this situation?
|
||||
PHP has Exceptions to define errors and the ability to "throw" them to stop code
|
||||
execution and tell the user of your code that something is wrong.
|
||||
```php
|
||||
<?php
|
||||
|
||||
class Processor
|
||||
{
|
||||
public function charge($creditCard)
|
||||
{
|
||||
if (strlen($creditCard->getNumber()) !== 16) {
|
||||
throw new Exception('Credit card is not right');
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In this case, if someone tried to use the Processor class
|
||||
to charge a credit card number that is not 16 characters long, an
|
||||
exception will be thrown which stops the rest of the code from running.
|
||||
```php
|
||||
$processor = new Processor();
|
||||
$processor->charge('1234');
|
||||
```
|
||||
|
||||
A developer who wants to prevent an exception from stopping code execution
|
||||
can catch the exception and use it for logging or display the error to a user.
|
||||
|
||||
Just wrap the code that might throw an exception with the keyword "try" and brackets
|
||||
followed by "catch", the exception type in parentheses and more brackets.
|
||||
```php
|
||||
try {
|
||||
$processor->charge('1234');
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage() . "\n";
|
||||
}
|
||||
```
|
||||
|
||||
You can make your own custom exceptions as well. They are just classes
|
||||
that extend Exception.
|
||||
```php
|
||||
class MyCustomException extends Exception {}
|
||||
```
|
||||
|
||||
Then, you can try to catch your exception instead of the base exception.
|
||||
```php
|
||||
try {
|
||||
throw new MyCustomException('I am a custom exception');
|
||||
} catch (MyCustomException $e) {
|
||||
echo "Cauth MyCustomException\n";
|
||||
}
|
||||
```
|
||||
|
||||
Since all exceptions inherit from Exception, catching
|
||||
Exception will catch any and all exceptions that might be thrown.
|
||||
```php
|
||||
try {
|
||||
throw new MyCustomException('I inherit from Exception');
|
||||
} catch (Exception $e) {
|
||||
echo "Still caught MyCustomException\n";
|
||||
}
|
||||
```
|
||||
|
||||
You can also create multiple catch blocks to handle different types of exceptions in
|
||||
one try-catch.
|
||||
```php
|
||||
try {
|
||||
throw new MyCustomException('I am being thrown again');
|
||||
} catch (MyCustomException $e) {
|
||||
echo "MyCustomException was caught\n";
|
||||
} catch (Exception $e) {
|
||||
echo "Just in case a different exception is thrown\n";
|
||||
}
|
||||
```
|
62
chapters/functions.md
Normal file
62
chapters/functions.md
Normal file
@ -0,0 +1,62 @@
|
||||
A function allows you to store code under a name and then execute
|
||||
that code later.
|
||||
|
||||
A function always starts with the
|
||||
function keyword followed by the name with parentheses and then
|
||||
opening and closing curly braces around the code.
|
||||
```php
|
||||
<?php
|
||||
|
||||
function hello_world() {
|
||||
echo "hello world\n";
|
||||
}
|
||||
```
|
||||
|
||||
To call the function, use the function name with parentheses.
|
||||
```php
|
||||
hello_world();
|
||||
```
|
||||
|
||||
You can set up values to be passed into a function.
|
||||
To do so, write variables in between the function parentheses.
|
||||
Each one should be separated by a comma.
|
||||
```php
|
||||
function greet($firstname, $lastname) {
|
||||
echo "hello $firstname $lastname\n";
|
||||
}
|
||||
```
|
||||
|
||||
Then, you can pass in values when calling a function. In the greet function,
|
||||
'John' is assigned to $firstname and 'Smith' is assigned to
|
||||
$lastname.
|
||||
```php
|
||||
greet('John', 'Smith');
|
||||
```
|
||||
|
||||
You can also return a value from a function. You can only
|
||||
return a single value from a function.
|
||||
```php
|
||||
function capitalize($value) {
|
||||
return strtoupper($value);
|
||||
}
|
||||
```
|
||||
|
||||
When calling a function, it will output the return value which
|
||||
you can load into a variable.
|
||||
```php
|
||||
$animal = capitalize('dog');
|
||||
echo "$animal\n";
|
||||
```
|
||||
|
||||
You can also create nameless functions called closures. Closures can be
|
||||
stored in variables or passed into other functions.
|
||||
```php
|
||||
$sum = function ($a, $b) {
|
||||
return $a + $b;
|
||||
};
|
||||
```
|
||||
|
||||
You can execute a closure by putting parentheses after the variable.
|
||||
```php
|
||||
echo $sum(1, 2) . "\n";
|
||||
```
|
60
chapters/interfaces.md
Normal file
60
chapters/interfaces.md
Normal file
@ -0,0 +1,60 @@
|
||||
The word "interface" is a confusing term because it is used for so many different concepts.
|
||||
Most often, we use it to describe the appearance of an app and how a user interacts with it.
|
||||
However, in PHP, an interface is a special construct that acts as a contract for classes.
|
||||
An interface defines what methods a class should have.
|
||||
```php
|
||||
<?php
|
||||
|
||||
interface Chair
|
||||
{
|
||||
public function setColor($color);
|
||||
public function setLegs($number);
|
||||
}
|
||||
```
|
||||
|
||||
To use an interface with a class, you use the "implements" keyword after the class name.
|
||||
Now, the Recliner class must have a setColor method and a setLegs method.
|
||||
If you do not create the required methods on the class, PHP will throw an error.
|
||||
```php
|
||||
class Recliner implements Chair
|
||||
{
|
||||
private $color;
|
||||
private $legs;
|
||||
|
||||
public function setColor($color)
|
||||
{
|
||||
$this->color = $color;
|
||||
}
|
||||
|
||||
public function setLegs($number)
|
||||
{
|
||||
$this->legs = $number;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Interfaces are helpful when you are using code created by someone else.
|
||||
For example, another developer may have created code that manages online payments, but they want to give you
|
||||
the ability to create your own payment class that works with their code. In that case, the developer
|
||||
creates an interface with all the required methods they need to charge a payment. The interface
|
||||
becomes a contract between your code and the other developer's code to work a certain way.
|
||||
```php
|
||||
interface Payment
|
||||
{
|
||||
public function charge($amount);
|
||||
}
|
||||
|
||||
class CreditCard
|
||||
{
|
||||
public function charge($amount)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Since CreditCard implements Payment, other developers can use the charge method, knowing it exists on the class.
|
||||
```php
|
||||
$creditCard = new CreditCard();
|
||||
$creditCard->charge(25);
|
||||
```
|
69
chapters/loops.md
Normal file
69
chapters/loops.md
Normal file
@ -0,0 +1,69 @@
|
||||
A loop tells PHP to run a block of code more than once.
|
||||
A classic loop is a while loop.
|
||||
A "while" loop will continue to run the block of code as long as the value in parentheses is true.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$num = 5;
|
||||
while ($num > 0) {
|
||||
echo "While loop $num\n";
|
||||
--$num;
|
||||
}
|
||||
```
|
||||
|
||||
A "do while" loop is similar to a "while" loop except it always runs at least
|
||||
one iteration. In a classic "while" loop, no iterations may be executed if
|
||||
the value in parentheses is false. In a "do while", the boolean check
|
||||
is not done until after the execution of an iteration.
|
||||
```php
|
||||
$num = 0;
|
||||
do {
|
||||
echo "Do while $num\n";
|
||||
++$num;
|
||||
} while ($num < 5);
|
||||
```
|
||||
|
||||
"for" loops allow you to create a more concise while loop.
|
||||
Inside the parentheses, the left section creates a variable before the loop
|
||||
starts, the middle section is the check that is done at the beginning of each loop
|
||||
and the third section is executed after each loop.
|
||||
```php
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
echo "For loop $i\n";
|
||||
}
|
||||
```
|
||||
|
||||
A "foreach" loop allows you to easily loop over an array.
|
||||
An array is a list of data stored together.
|
||||
The "as" keyword lets you assign a variable to the value
|
||||
in the array for the current iteration of the loop.
|
||||
```php
|
||||
$set = [1, 2, 3, 4, 5];
|
||||
foreach ($set as $num) {
|
||||
echo "Array value $num\n";
|
||||
}
|
||||
```
|
||||
|
||||
In loops, you can use the keyword "break" to stop the loop execution
|
||||
no matter how many more iterations should run.
|
||||
```php
|
||||
$values = ['one', 'two', 'three'];
|
||||
foreach ($values as $value) {
|
||||
if ($value === 'two') {
|
||||
break;
|
||||
}
|
||||
echo "Break $value\n";
|
||||
}
|
||||
```
|
||||
|
||||
The "continue" keyword stops executing the current loop iteration,
|
||||
but then allows the loop to continue with other iterations.
|
||||
```php
|
||||
$values = ['one', 'skip', 'three'];
|
||||
foreach ($values as $value) {
|
||||
if ($value === 'skip') {
|
||||
continue;
|
||||
}
|
||||
echo "Continue $value\n";
|
||||
}
|
||||
```
|
81
chapters/static.md
Normal file
81
chapters/static.md
Normal file
@ -0,0 +1,81 @@
|
||||
When writing a class, all of the properties and methods are being defined for the object
|
||||
that will be created from the class.
|
||||
```php
|
||||
<?php
|
||||
|
||||
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.
|
||||
```php
|
||||
$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.
|
||||
```php
|
||||
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.
|
||||
```php
|
||||
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.
|
||||
```php
|
||||
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.
|
||||
```php
|
||||
$house = TinyHouse::build('Blue', 4, true);
|
||||
```
|
31
chapters/strings.md
Normal file
31
chapters/strings.md
Normal file
@ -0,0 +1,31 @@
|
||||
As seen in the first chapter, a string is a group of characters created by
|
||||
surrounding text in single or double quotes.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$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.
|
||||
```php
|
||||
echo "Jacob\nJones\n";
|
||||
```
|
||||
|
||||
Double quoted strings can also embed variables in the text. This code
|
||||
outputs "Cindy Smith".
|
||||
```php
|
||||
$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.
|
||||
```php
|
||||
$firstname = 'Jenny';
|
||||
$lastname = 'Madison';
|
||||
$fullname = $firstname . $lastname;
|
||||
echo $fullname;
|
||||
```
|
44
chapters/variables.md
Normal file
44
chapters/variables.md
Normal file
@ -0,0 +1,44 @@
|
||||
|
||||
The variable is the basic building block of any programming language.
|
||||
In PHP, all variables start with a dollar sign.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$greeting;
|
||||
```
|
||||
|
||||
To set data in a variable, you put an equals sign after it and some data.
|
||||
```php
|
||||
$greeting = 'Hello World!';
|
||||
```
|
||||
|
||||
Once you create a variable, you can use it again in other commands and functions.
|
||||
```php
|
||||
echo $greeting;
|
||||
```
|
||||
|
||||
After the dollar sign, a PHP variable must have an alphabetic character or underscore. Also, variables are case sensitive.
|
||||
```php
|
||||
$_var = 'I am a variable with an underscore!';
|
||||
$Var = 'I am a variable with a capital letter!';
|
||||
$var = 'I am a new variable';
|
||||
```
|
||||
|
||||
Variables can hold many different types of data, but there are four simple ones you can try now.
|
||||
An int is a number without a decimal place.
|
||||
A float is a number with a decimal place.
|
||||
A boolean can be two values: true or false.
|
||||
Last, there is a string: a collection of characters.
|
||||
```php
|
||||
$int = 1;
|
||||
$float = 100.10;
|
||||
$bool = true;
|
||||
$string = 'I am a string';
|
||||
```
|
||||
|
||||
In other programming languages, you have to write what type of data the variable will contain.
|
||||
PHP keeps it simple by allowing you to put any type of data in a variable, including already used variables.
|
||||
```php
|
||||
$number = 1;
|
||||
$number = 'one';
|
||||
```
|
@ -1,48 +0,0 @@
|
||||
<?php
|
||||
|
||||
// Abstract classes are similar to interfaces in that they define methods that a sub-class must implement.
|
||||
// However, an abstract class can also have normal methods. To create an abstract class, use the "abstract"
|
||||
// keyword followed by class and the name of the class.
|
||||
abstract class CellPhone
|
||||
{
|
||||
abstract public function unlock();
|
||||
|
||||
public function turnOn()
|
||||
{
|
||||
echo "Holding power button...\n";
|
||||
}
|
||||
}
|
||||
|
||||
// To use an abstract class, you create another class that extends it and create any methods that were marked as abstract.
|
||||
// A class can only extend one abstract class and the child class has to implement all abstract methods.
|
||||
class iPhone extends CellPhone
|
||||
{
|
||||
public function unlock()
|
||||
{
|
||||
echo "Touching fingerprint reader...\n";
|
||||
}
|
||||
}
|
||||
|
||||
// In this example, we use an abstract class to create the behavior for turning on a cell phone and then
|
||||
// force the child classes to implement how to unlock the phone. We have clearly defined what a cell phone
|
||||
// performs and we have limited code duplication.
|
||||
class Android extends CellPhone
|
||||
{
|
||||
public function unlock()
|
||||
{
|
||||
echo "Typing passcode...\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Our iPhone and Android classes can now both use the turnOn method and the unlock method.
|
||||
$iPhone = new iPhone();
|
||||
$iPhone->turnOn();
|
||||
$iPhone->unlock();
|
||||
|
||||
$android = new Android();
|
||||
$android->turnOn();
|
||||
$android->unlock();
|
||||
|
||||
// Lastly, you cannot create an instance of an abstract class. PHP would not know how to use the abstract methods
|
||||
// so when you try to create an abstract instance you will get an error.
|
||||
$cellPhone = new CellPhone();
|
@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
// Now that we know how to create variables, let's look at doing some math.
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
|
||||
// To add two values, use the plus symbol.
|
||||
echo $a + $b;
|
||||
|
||||
// To subtract, use the minus symbol.
|
||||
echo $b - $a;
|
||||
|
||||
// To multiply two values, use an asterisk.
|
||||
echo $a * $b;
|
||||
|
||||
// To divide values, use a forward slash.
|
||||
echo $b / $a;
|
||||
|
||||
// PHP uses the percent symbol for calculating the modulus of two numbers.
|
||||
// The modulus is calculated by dividing two numbers and returning the remainder of the result.
|
||||
// In this example, the value of $mod will be 0.
|
||||
$mod = 10 % 5;
|
||||
|
||||
// You can also use double asterisks to calculate a number to the power of another number.
|
||||
// The following statement will print 25.
|
||||
echo 5 ** 2;
|
@ -1,28 +0,0 @@
|
||||
<?php
|
||||
|
||||
// In PHP, arrays are used to store a list of items in a single variable.
|
||||
// There are two ways to create an array.
|
||||
|
||||
// First, you can use the array construct to pass in values separated by commas
|
||||
// and it will return an array.
|
||||
$taskList = array('grocery store', 'change car oil');
|
||||
|
||||
// Second, you can surround the list in square brackets.
|
||||
// This style is the most common and recommended form
|
||||
// of creating an array.
|
||||
$groceryList = ['bread', 'milk', 'eggs'];
|
||||
|
||||
// PHP will automatically assign index keys for each value in an array
|
||||
// starting with 0. So, to access a value in an array you will
|
||||
// pass the key number into brackets after the variable name.
|
||||
echo $groceryList[0] . "\n";
|
||||
echo $groceryList[1] . "\n";
|
||||
|
||||
// You can also assign keys in an array using numbers or strings.
|
||||
// It is very common to create an array with string keys. The pattern
|
||||
// is called an associative array or a map.
|
||||
$car = ['make' => 'Toyota', 'model' => 'Camry'];
|
||||
|
||||
// To access the value in an associative array, just use the string key in brackets
|
||||
// after the variable name.
|
||||
echo $car['model'] . "\n";
|
@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
// In the tradition of our ancestors, let's start with a hello world program.
|
||||
// All PHP files must start with a <?php tag unless it is for a html template.
|
||||
// (We will learn about html templates later.)
|
||||
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\n";
|
||||
|
||||
// Second, PHP stores text in strings.
|
||||
|
||||
// To write a string, you surround letters with single or double quotes.
|
||||
// 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";
|
@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
// Boolean logic is used to combine booleans to return another boolean.
|
||||
|
||||
// Using double ampersands tells PHP to check if both values are true.
|
||||
// If so, it will return true. If not, it will return false.
|
||||
$a = true;
|
||||
$b = true;
|
||||
$c = false;
|
||||
|
||||
// Returns true.
|
||||
$a && $b;
|
||||
// Returns false.
|
||||
$a && $c;
|
||||
|
||||
// Using two pipe characters checks if either value is true.
|
||||
// Then, it will return true. If both values are false, then PHP
|
||||
// returns false.
|
||||
$a = true;
|
||||
$b = false;
|
||||
$c = false;
|
||||
$d = true;
|
||||
|
||||
// Returns true.
|
||||
$a || $b;
|
||||
// Returns false.
|
||||
$b || $c;
|
||||
// Returns true.
|
||||
$a || $d;
|
||||
|
||||
// Using an exclamation point returns the value flipped.
|
||||
$d = true;
|
||||
|
||||
// Outputs false.
|
||||
echo !$d;
|
@ -1,47 +0,0 @@
|
||||
<?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');
|
@ -1,62 +0,0 @@
|
||||
<?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";
|
||||
}
|
||||
}
|
||||
|
||||
// The drive function now 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();
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
// In the last chapter, we defined properties and methods on the class using the public keyword.
|
||||
// You can also define them using the "protected" and "private" keywords.
|
||||
// Both keywords prevent the properties and functions from being accessible outside the object.
|
||||
// Only the object itself can use each.
|
||||
class Phone
|
||||
{
|
||||
private $number;
|
||||
|
||||
public function setNumber($number)
|
||||
{
|
||||
$this->number = $number;
|
||||
}
|
||||
}
|
||||
|
||||
// We cannot set the number using "$phone->number = '123-456-7890'".
|
||||
// Instead, we can use the public method.
|
||||
$phone = new Phone();
|
||||
$phone->setNumber('123-456-7890');
|
||||
|
||||
// Making an attribute or function private, gives you more control over the data in the object.
|
||||
// For example, we could prevent a number being set if it starts with a 7.
|
||||
class Phone2
|
||||
{
|
||||
private $number;
|
||||
|
||||
public function setNumber($number)
|
||||
{
|
||||
if (substr($number, 0, 1) !== '7') {
|
||||
$this->number = $number;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 class.
|
||||
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 "number" property
|
||||
// because it is still listed as private.
|
||||
class Smartphone extends Phone3
|
||||
{
|
||||
public function setCaller($caller)
|
||||
{
|
||||
$this->caller = $caller;
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
// Classes allow you to define your own data types. All classes start with the
|
||||
// class keyword followed by the name of the class and opening and closing curly braces.
|
||||
class Car
|
||||
{
|
||||
}
|
||||
|
||||
// To create an instance of a class, you use the "new" keyword in front of the class name
|
||||
// with parentheses.
|
||||
$car = new Car();
|
||||
|
||||
// A class can define attributes and methods. An attribute is a piece of data
|
||||
// stored on the class instance. You can define an attribute by adding the
|
||||
// word "public" and a variable name inside the class definition.
|
||||
class Bicycle
|
||||
{
|
||||
public $color;
|
||||
}
|
||||
|
||||
// Then, when you create an instance of the class, you can set and use
|
||||
// the color attribute on the bicycle using "->".
|
||||
$bike = new Bicycle();
|
||||
$bike->color = 'Blue';
|
||||
echo $bike->color . "\n";
|
||||
|
||||
// An instance of a class is called an object. Congratulations!
|
||||
// You are now performing object-oriented development.
|
||||
$redBike = new Bicycle();
|
||||
$redBike->color = 'Red';
|
||||
echo $redBike->color . " Bike Object\n";
|
||||
|
||||
// A method is a function attached to the class. You can add a method
|
||||
// to a class by using the "public" keyword followed by the function. A method
|
||||
// can access the attributes and methods of an object instance using the "$this" variable.
|
||||
class Tricycle
|
||||
{
|
||||
public $color;
|
||||
|
||||
public function echoColor()
|
||||
{
|
||||
echo $this->color . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// You can execute a method on an object using the same "->" arrow characters.
|
||||
$bike = new Tricycle();
|
||||
$bike->color = 'Red';
|
||||
$bike->echoColor();
|
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
// A boolean is a value that is always 0 or 1, yes or no, on or off.
|
||||
// In PHP, a boolean is represented by the words true and false.
|
||||
// While programming, you will often want to know if something is positive or negative.
|
||||
$a = true;
|
||||
$b = false;
|
||||
|
||||
// There are many constructs and functions that will return a boolean.
|
||||
// To start, let's look at comparisons.
|
||||
$one = 1;
|
||||
$two = 2;
|
||||
|
||||
// Double equals checks if two values are equal.
|
||||
// This statement will return false.
|
||||
$one == $two;
|
||||
|
||||
// An exclamation point and equal sign check if two values are not equal.
|
||||
// This statement will return true.
|
||||
$one != $two;
|
||||
|
||||
// You can use greater than and less than symbols to check for comparisons too.
|
||||
// This statement will return false.
|
||||
$one > $two;
|
||||
|
||||
// This statement will return true.
|
||||
$one < $two;
|
||||
|
||||
// If you combine a greater than or less than symbol with an equal,
|
||||
// it will check if the value is greater or less than or equal to another value.
|
||||
$one <= $two;
|
||||
$one >= $two;
|
||||
|
||||
// You can also check that two values are equal and of the same type
|
||||
// by using three equal signs.
|
||||
|
||||
// This returns true.
|
||||
1 == 1;
|
||||
1 == '1';
|
||||
1 == true;
|
||||
1 == 1.0;
|
||||
1 === 1;
|
||||
|
||||
// This returns false.
|
||||
1 === '1';
|
||||
1 === true;
|
||||
1 === 1.0;
|
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
// When writing code, there will be times when you need to perform actions only under certain circumstances.
|
||||
// There are several ways to control execution in PHP.
|
||||
// We will start with an if statement.
|
||||
$animal = 'cow';
|
||||
if ($animal == 'cow') {
|
||||
echo "Moooooo.....\n";
|
||||
}
|
||||
|
||||
// All conditionals check to see if a statement evaluates to true or false.
|
||||
// In the case above, since $animal equals 'cow', the statement returns true and the contents of the if statement are executed.
|
||||
|
||||
|
||||
// An if statement can have multiple conditions chained together.
|
||||
// If the first if statement returns false, then PHP will check each elseif.
|
||||
// If none of the checks return true, then the else block will be executed.
|
||||
$animal = 'bird';
|
||||
if ($animal == 'dog') {
|
||||
echo "Woof! 🐶\n";
|
||||
} elseif ($animal == 'cat') {
|
||||
echo "Meow!? 🐱\n";
|
||||
} elseif ($animal == 'bird') {
|
||||
echo "Chirp! 🐦\n";
|
||||
} else {
|
||||
echo "I am not a dog, cat or bird\n";
|
||||
}
|
||||
|
||||
// An alternative to the if statement is the switch.
|
||||
// A switch statement has multiple cases to check if the value in parentheses equals something.
|
||||
// In this statement, since $food equals 'apples', the switch will echo "Eating an apple".
|
||||
// The default case will be run if no other case evaluates to true, like an else statement.
|
||||
$food = 'apples';
|
||||
switch ($food) {
|
||||
case 'apples':
|
||||
echo "Eating an apple\n";
|
||||
break;
|
||||
case 'oranges':
|
||||
echo "Eating an orange\n";
|
||||
break;
|
||||
case 'peaches':
|
||||
echo "Eating a peach\n";
|
||||
break;
|
||||
default:
|
||||
echo "No food, I am hungry\n";
|
||||
}
|
||||
|
||||
// Breaks are a special keyword that tell PHP to stop execution once a case passes.
|
||||
// If you do not use a break, PHP will continue to execute all following cases.
|
||||
// In this switch, both "Drinking water" and "Drinking tea" will be executed since there is no break in the 'water' case.
|
||||
$drink = 'water';
|
||||
switch ($drink) {
|
||||
case 'water':
|
||||
echo "Drinking water\n";
|
||||
case 'tea':
|
||||
echo "Drinking tea\n";
|
||||
break;
|
||||
}
|
||||
|
||||
// PHP also supports single line conditionals called a ternary.
|
||||
// In a ternary, the condition is followed by a question mark before the value that should be returned if the condition is true and then another colon and a value to return if the condition is false.
|
||||
$language = 'english';
|
||||
echo $language == 'spanish' ? "hola\n" : "hello\n";
|
||||
|
||||
// Lastly, there is another form of a ternary that checks if a value is set and then returns the value to the right of the two question marks if the value is null.
|
||||
echo $IDoNotExist ?? "Variable not set\n";
|
||||
|
||||
// You can also chain multiple checks in a row.
|
||||
$IExist = "Variable exists\n";
|
||||
echo $IDoNotExist ?? $IExist ?? "Neither variable is set\n";
|
@ -1,60 +0,0 @@
|
||||
<?php
|
||||
|
||||
// Sometimes things go wrong when someone uses your code. How do we handle this situation?
|
||||
// PHP has Exceptions to define errors and the ability to "throw" them to stop code
|
||||
// execution and tell the user of your code that something is wrong.
|
||||
class Processor
|
||||
{
|
||||
public function charge($creditCard)
|
||||
{
|
||||
if (strlen($creditCard->getNumber()) !== 16) {
|
||||
throw new Exception('Credit card is not right');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// In this case, if someone tried to use the Processor class
|
||||
// to charge a credit card number that is not 16 characters long, an
|
||||
// exception will be thrown which stops the rest of the code from running.
|
||||
$processor = new Processor();
|
||||
$processor->charge('1234');
|
||||
|
||||
// A developer who wants to prevent an exception from stopping code execution
|
||||
// can catch the exception and use it for logging or display the error to a user.
|
||||
|
||||
// Just wrap the code that might throw an exception with the keyword "try" and brackets
|
||||
// followed by "catch", the exception type in parentheses and more brackets.
|
||||
try {
|
||||
$processor->charge('1234');
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage() . "\n";
|
||||
}
|
||||
|
||||
// You can make your own custom exceptions as well. They are just classes
|
||||
// that extend Exception.
|
||||
class MyCustomException extends Exception {}
|
||||
|
||||
// Then, you can try to catch your exception instead of the base exception.
|
||||
try {
|
||||
throw new MyCustomException('I am a custom exception');
|
||||
} catch (MyCustomException $e) {
|
||||
echo "Cauth MyCustomException\n";
|
||||
}
|
||||
|
||||
// Since all exceptions inherit from Exception, catching
|
||||
// Exception will catch any and all exceptions that might be thrown.
|
||||
try {
|
||||
throw new MyCustomException('I inherit from Exception');
|
||||
} catch (Exception $e) {
|
||||
echo "Still caught MyCustomException\n";
|
||||
}
|
||||
|
||||
// You can also create multiple catch blocks to handle different types of exceptions in
|
||||
// one try-catch.
|
||||
try {
|
||||
throw new MyCustomException('I am being thrown again');
|
||||
} catch (MyCustomException $e) {
|
||||
echo "MyCustomException was caught\n";
|
||||
} catch (Exception $e) {
|
||||
echo "Just in case a different exception is thrown\n";
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
// A function allows you to store code under a name and then execute
|
||||
// that code later.
|
||||
|
||||
// A function always starts with the
|
||||
// function keyword followed by the name with parentheses and then
|
||||
// opening and closing curly braces around the code.
|
||||
function hello_world() {
|
||||
echo "hello world\n";
|
||||
}
|
||||
|
||||
// To call the function, use the function name with parentheses.
|
||||
hello_world();
|
||||
|
||||
// You can set up values to be passed into a function.
|
||||
// To do so, write variables in between the function parentheses.
|
||||
// Each one should be separated by a comma.
|
||||
function greet($firstname, $lastname) {
|
||||
echo "hello $firstname $lastname\n";
|
||||
}
|
||||
|
||||
// Then, you can pass in values when calling a function. In the greet function,
|
||||
// 'John' is assigned to $firstname and 'Smith' is assigned to
|
||||
// $lastname.
|
||||
greet('John', 'Smith');
|
||||
|
||||
// You can also return a value from a function. You can only
|
||||
// return a single value from a function.
|
||||
function capitalize($value) {
|
||||
return strtoupper($value);
|
||||
}
|
||||
|
||||
// When calling a function, it will output the return value which
|
||||
// you can load into a variable.
|
||||
$animal = capitalize('dog');
|
||||
echo "$animal\n";
|
||||
|
||||
// You can also create nameless functions called closures. Closures can be
|
||||
// stored in variables or passed into other functions.
|
||||
$sum = function ($a, $b) {
|
||||
return $a + $b;
|
||||
};
|
||||
|
||||
// You can execute a closure by putting parentheses after the variable.
|
||||
echo $sum(1, 2) . "\n";
|
@ -1,52 +0,0 @@
|
||||
<?php
|
||||
|
||||
// The word "interface" is a confusing term because it is used for so many different concepts.
|
||||
// Most often, we use it to describe the appearance of an app and how a user interacts with it.
|
||||
// However, in PHP, an interface is a special construct that acts as a contract for classes.
|
||||
// An interface defines what methods a class should have.
|
||||
interface Chair
|
||||
{
|
||||
public function setColor($color);
|
||||
public function setLegs($number);
|
||||
}
|
||||
|
||||
// To use an interface with a class, you use the "implements" keyword after the class name.
|
||||
// Now, the Recliner class must have a setColor method and a setLegs method.
|
||||
// If you do not create the required methods on the class, PHP will throw an error.
|
||||
class Recliner implements Chair
|
||||
{
|
||||
private $color;
|
||||
private $legs;
|
||||
|
||||
public function setColor($color)
|
||||
{
|
||||
$this->color = $color;
|
||||
}
|
||||
|
||||
public function setLegs($number)
|
||||
{
|
||||
$this->legs = $number;
|
||||
}
|
||||
}
|
||||
|
||||
// Interfaces are helpful when you are using code created by someone else.
|
||||
// For example, another developer may have created code that manages online payments, but they want to give you
|
||||
// the ability to create your own payment class that works with their code. In that case, the developer
|
||||
// creates an interface with all the required methods they need to charge a payment. The interface
|
||||
// becomes a contract between your code and the other developer's code to work a certain way.
|
||||
interface Payment
|
||||
{
|
||||
public function charge($amount);
|
||||
}
|
||||
|
||||
class CreditCard
|
||||
{
|
||||
public function charge($amount)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Since CreditCard implements Payment, other developers can use the charge method, knowing it exists on the class.
|
||||
$creditCard = new CreditCard();
|
||||
$creditCard->charge(25);
|
@ -1,57 +0,0 @@
|
||||
<?php
|
||||
|
||||
// A loop tells PHP to run a block of code more than once.
|
||||
// A classic loop is a while loop.
|
||||
// A "while" loop will continue to run the block of code as long as the value in parentheses is true.
|
||||
$num = 5;
|
||||
while ($num > 0) {
|
||||
echo "While loop $num\n";
|
||||
--$num;
|
||||
}
|
||||
|
||||
// A "do while" loop is similar to a "while" loop except it always runs at least
|
||||
// one iteration. In a classic "while" loop, no iterations may be executed if
|
||||
// the value in parentheses is false. In a "do while", the boolean check
|
||||
// is not done until after the execution of an iteration.
|
||||
$num = 0;
|
||||
do {
|
||||
echo "Do while $num\n";
|
||||
++$num;
|
||||
} while ($num < 5);
|
||||
|
||||
// "for" loops allow you to create a more concise while loop.
|
||||
// Inside the parentheses, the left section creates a variable before the loop
|
||||
// starts, the middle section is the check that is done at the beginning of each loop
|
||||
// and the third section is executed after each loop.
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
echo "For loop $i\n";
|
||||
}
|
||||
|
||||
// A "foreach" loop allows you to easily loop over an array.
|
||||
// An array is a list of data stored together.
|
||||
// The "as" keyword lets you assign a variable to the value
|
||||
// in the array for the current iteration of the loop.
|
||||
$set = [1, 2, 3, 4, 5];
|
||||
foreach ($set as $num) {
|
||||
echo "Array value $num\n";
|
||||
}
|
||||
|
||||
// In loops, you can use the keyword "break" to stop the loop execution
|
||||
// no matter how many more iterations should run.
|
||||
$values = ['one', 'two', 'three'];
|
||||
foreach ($values as $value) {
|
||||
if ($value === 'two') {
|
||||
break;
|
||||
}
|
||||
echo "Break $value\n";
|
||||
}
|
||||
|
||||
// The "continue" keyword stops executing the current loop iteration,
|
||||
// but then allows the loop to continue with other iterations.
|
||||
$values = ['one', 'skip', 'three'];
|
||||
foreach ($values as $value) {
|
||||
if ($value === 'skip') {
|
||||
continue;
|
||||
}
|
||||
echo "Continue $value\n";
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
<?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);
|
@ -1,23 +0,0 @@
|
||||
<?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;
|
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
// The variable is the basic building block of any programming language.
|
||||
// In PHP, all variables start with a dollar sign.
|
||||
$greeting;
|
||||
|
||||
// To set data in a variable, you put an equals sign after it and some data.
|
||||
$greeting = 'Hello World!';
|
||||
|
||||
// Once you create a variable, you can use it again in other commands and functions.
|
||||
echo $greeting;
|
||||
|
||||
// After the dollar sign, a PHP variable must have an alphabetic character or underscore. Also, variables are case sensitive.
|
||||
$_var = 'I am a variable with an underscore!';
|
||||
$Var = 'I am a variable with a capital letter!';
|
||||
$var = 'I am a new variable';
|
||||
|
||||
// Variables can hold many different types of data, but there are four simple ones you can try now.
|
||||
// An int is a number without a decimal place.
|
||||
// A float is a number with a decimal place.
|
||||
// A boolean can be two values: true or false.
|
||||
// Last, there is a string: a collection of characters.
|
||||
$int = 1;
|
||||
$float = 100.10;
|
||||
$bool = true;
|
||||
$string = 'I am a string';
|
||||
|
||||
// In other programming languages, you have to write what type of data the variable will contain.
|
||||
// PHP keeps it simple by allowing you to put any type of data in a variable, including already used variables.
|
||||
$number = 1;
|
||||
$number = 'one';
|
@ -3,7 +3,8 @@
|
||||
"description": "A site for learning PHP",
|
||||
"require": {
|
||||
"php": "^7.1.3",
|
||||
"symfony/console": "^4.1"
|
||||
"symfony/console": "^4.1",
|
||||
"erusev/parsedown": "^1.7"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^7.3"
|
||||
|
48
composer.lock
generated
48
composer.lock
generated
@ -4,8 +4,54 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "3b082f5c5185a26da689e29868b00d2a",
|
||||
"content-hash": "34fcbe4d710da9cb9a842886e3ff38e8",
|
||||
"packages": [
|
||||
{
|
||||
"name": "erusev/parsedown",
|
||||
"version": "1.7.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/erusev/parsedown.git",
|
||||
"reference": "92e9c27ba0e74b8b028b111d1b6f956a15c01fc1"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/erusev/parsedown/zipball/92e9c27ba0e74b8b028b111d1b6f956a15c01fc1",
|
||||
"reference": "92e9c27ba0e74b8b028b111d1b6f956a15c01fc1",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"ext-mbstring": "*",
|
||||
"php": ">=5.3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^4.8.35"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"Parsedown": ""
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Emanuil Rusev",
|
||||
"email": "hello@erusev.com",
|
||||
"homepage": "http://erusev.com"
|
||||
}
|
||||
],
|
||||
"description": "Parser for Markdown.",
|
||||
"homepage": "http://parsedown.org",
|
||||
"keywords": [
|
||||
"markdown",
|
||||
"parser"
|
||||
],
|
||||
"time": "2018-03-08T01:11:30+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/console",
|
||||
"version": "v4.1.4",
|
||||
|
40
config.php
40
config.php
@ -12,10 +12,10 @@ return [
|
||||
|
||||
/*
|
||||
*
|
||||
* Directory holding code files used in examples
|
||||
* Directory holding chapter files
|
||||
*
|
||||
*/
|
||||
'code_dir' => __DIR__ . '/code',
|
||||
'chapter_dir' => __DIR__ . '/chapters',
|
||||
|
||||
/*
|
||||
*
|
||||
@ -48,108 +48,108 @@ return [
|
||||
Page::create('installing-php', 'installing-php.phtml'),
|
||||
Page::create('credits', 'credits.phtml'),
|
||||
Page::create('404', '404.phtml'),
|
||||
Page::create('basics', null, 'basics.php', [
|
||||
Page::create('basics', null, 'basics.md', [
|
||||
'title' => 'Basics',
|
||||
'subtitle' => 'Getting started',
|
||||
'next' => 'variables',
|
||||
]),
|
||||
Page::create('variables', null, 'variables.php', [
|
||||
Page::create('variables', null, 'variables.md', [
|
||||
'title' => 'Variables',
|
||||
'subtitle' => 'The building blocks of PHP',
|
||||
'previous' => 'basics',
|
||||
'next' => 'arithmetic',
|
||||
]),
|
||||
Page::create('arithmetic', null, 'arithmetic.php', [
|
||||
Page::create('arithmetic', null, 'arithmetic.md', [
|
||||
'title' => 'Arithmetic',
|
||||
'subtitle' => 'Doing math like a pro',
|
||||
'previous' => 'variables',
|
||||
'next' => 'strings',
|
||||
]),
|
||||
Page::create('strings', null, 'strings.php', [
|
||||
Page::create('strings', null, 'strings.md', [
|
||||
'title' => 'Strings',
|
||||
'subtitle' => 'Working with text',
|
||||
'previous' => 'arithmetic',
|
||||
'next' => 'comparisons',
|
||||
]),
|
||||
Page::create('comparisons', null, 'comparisons.php', [
|
||||
Page::create('comparisons', null, 'comparisons.md', [
|
||||
'title' => 'Comparisons',
|
||||
'subtitle' => 'Equality checking',
|
||||
'previous' => 'strings',
|
||||
'next' => 'boolean-logic',
|
||||
]),
|
||||
Page::create('boolean-logic', null, 'boolean-logic.php', [
|
||||
Page::create('boolean-logic', null, 'boolean-logic.md', [
|
||||
'title' => 'Boolean Logic',
|
||||
'subtitle' => 'Is it a yes or a no?',
|
||||
'previous' => 'comparisons',
|
||||
'next' => 'conditionals',
|
||||
]),
|
||||
Page::create('conditionals', null, 'conditionals.php', [
|
||||
Page::create('conditionals', null, 'conditionals.md', [
|
||||
'title' => 'Conditionals',
|
||||
'subtitle' => 'Checking the if before the what',
|
||||
'previous' => 'boolean-logic',
|
||||
'next' => 'loops',
|
||||
]),
|
||||
Page::create('loops', null, 'loops.php', [
|
||||
Page::create('loops', null, 'loops.md', [
|
||||
'title' => 'Loops',
|
||||
'subtitle' => 'Increase your repetitions',
|
||||
'previous' => 'conditionals',
|
||||
'next' => 'arrays',
|
||||
]),
|
||||
Page::create('arrays', null, 'arrays.php', [
|
||||
Page::create('arrays', null, 'arrays.md', [
|
||||
'title' => 'Arrays',
|
||||
'subtitle' => 'Time to make a list',
|
||||
'previous' => 'loops',
|
||||
'next' => 'functions',
|
||||
]),
|
||||
Page::create('functions', null, 'functions.php', [
|
||||
Page::create('functions', null, 'functions.md', [
|
||||
'title' => 'Functions',
|
||||
'subtitle' => 'Reusable code',
|
||||
'previous' => 'arrays',
|
||||
'next' => 'classes',
|
||||
]),
|
||||
Page::create('classes', null, 'classes.php', [
|
||||
Page::create('classes', null, 'classes.md', [
|
||||
'title' => 'Classes',
|
||||
'subtitle' => 'Object-oriented programming',
|
||||
'previous' => 'functions',
|
||||
'next' => 'classes-inheritance',
|
||||
]),
|
||||
Page::create('classes-inheritance', null, 'classes-inheritance.php', [
|
||||
Page::create('classes-inheritance', null, 'classes-inheritance.md', [
|
||||
'title' => 'Classes: Inheritance',
|
||||
'subtitle' => 'Extend your objects',
|
||||
'previous' => 'classes',
|
||||
'next' => 'classes-visibility',
|
||||
]),
|
||||
Page::create('classes-visibility', null, 'classes-visibility.php', [
|
||||
Page::create('classes-visibility', null, 'classes-visibility.md', [
|
||||
'title' => 'Classes: Visibility',
|
||||
'subtitle' => 'Privatizing your objects',
|
||||
'previous' => 'classes-inheritance',
|
||||
'next' => 'classes-constructor',
|
||||
]),
|
||||
Page::create('classes-constructor', null, 'classes-constructor.php', [
|
||||
Page::create('classes-constructor', null, 'classes-constructor.md', [
|
||||
'title' => 'Classes: Constructor',
|
||||
'subtitle' => 'Construct your objects',
|
||||
'previous' => 'classes-visibility',
|
||||
'next' => 'static',
|
||||
]),
|
||||
Page::create('static', null, 'static.php', [
|
||||
Page::create('static', null, 'static.md', [
|
||||
'title' => 'Static',
|
||||
'subtitle' => 'Class properties and methods',
|
||||
'previous' => 'classes-constructor',
|
||||
'next' => 'interfaces',
|
||||
]),
|
||||
Page::create('interfaces', null, 'interfaces.php', [
|
||||
Page::create('interfaces', null, 'interfaces.md', [
|
||||
'title' => 'Interfaces',
|
||||
'subtitle' => 'Writing code contracts',
|
||||
'previous' => 'static',
|
||||
'next' => 'abstract',
|
||||
]),
|
||||
Page::create('abstract', null, 'abstract.php', [
|
||||
Page::create('abstract', null, 'abstract.md', [
|
||||
'title' => 'Abstract Classes',
|
||||
'subtitle' => 'Inheriting an interface',
|
||||
'previous' => 'interfaces',
|
||||
'next' => 'exceptions',
|
||||
]),
|
||||
Page::create('exceptions', null, 'exceptions.php', [
|
||||
Page::create('exceptions', null, 'exceptions.md', [
|
||||
'title' => 'Exceptions',
|
||||
'subtitle' => 'Throwing errors',
|
||||
'previous' => 'abstract',
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Apprentice;
|
||||
|
||||
use Parsedown;
|
||||
|
||||
/**
|
||||
* Handles building pages in public folder
|
||||
* using configuration based on Page classes
|
||||
@ -95,13 +97,15 @@ 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);
|
||||
if (!empty($page->chapter)) {
|
||||
if (!file_exists(config('chapter_dir') . '/' . $page->chapter)) {
|
||||
throw new \Exception('Code file not found: ' . $page->chapter);
|
||||
}
|
||||
|
||||
$code = file_get_contents(config('code_dir') . '/' . $page->code);
|
||||
$page->variables['code'] = $code;
|
||||
$parser = new Parsedown();
|
||||
|
||||
$content = file_get_contents(config('chapter_dir') . '/' . $page->chapter);
|
||||
$page->variables['chapter'] = $parser->text($content);
|
||||
}
|
||||
|
||||
if ($page->template) {
|
||||
|
14
src/Page.php
14
src/Page.php
@ -23,12 +23,12 @@ class Page
|
||||
public $template;
|
||||
|
||||
/**
|
||||
* Name of code file to load as table
|
||||
* Name of chapter file to load and parse
|
||||
* Will be skipped if none is provided
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $code;
|
||||
public $chapter;
|
||||
|
||||
/**
|
||||
* Map of data to passed to template
|
||||
@ -42,18 +42,18 @@ class Page
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|null $template
|
||||
* @param string|null $code
|
||||
* @param string|null $chapter
|
||||
* @param array $variables
|
||||
*/
|
||||
public function __construct(
|
||||
string $name,
|
||||
?string $template = null,
|
||||
?string $code = null,
|
||||
?string $chapter = null,
|
||||
?array $variables = []
|
||||
) {
|
||||
$this->name = $name;
|
||||
$this->template = $template;
|
||||
$this->code = $code;
|
||||
$this->chapter = $chapter;
|
||||
$this->variables = $variables;
|
||||
}
|
||||
|
||||
@ -69,9 +69,9 @@ class Page
|
||||
public static function create(
|
||||
string $name,
|
||||
?string $template = null,
|
||||
?string $code = null,
|
||||
?string $chapter = null,
|
||||
?array $variables = []
|
||||
): Page {
|
||||
return new static($name, $template, $code, $variables);
|
||||
return new static($name, $template, $chapter, $variables);
|
||||
}
|
||||
}
|
||||
|
@ -61,65 +61,6 @@ function icon(string $name): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes string of PHP code and converts it into html for display
|
||||
*
|
||||
* @param string $code
|
||||
* @return string
|
||||
*/
|
||||
function code_table(string $code): string {
|
||||
$tokens = token_get_all($code);
|
||||
$output = '<div class="grid-code">' .
|
||||
'<div class="doc"></div>' .
|
||||
'<div class="code">' .
|
||||
'<pre>' .
|
||||
'<code class="language-php">';
|
||||
|
||||
$previousTokenWasComment = false;
|
||||
foreach ($tokens as $token) {
|
||||
if (is_string($token)) {
|
||||
$output .= $token;
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = $token[0];
|
||||
$text = $token[1];
|
||||
|
||||
if ($id == T_COMMENT || $id == T_DOC_COMMENT) {
|
||||
$text = htmlspecialchars(trim(str_replace('/', '', $text)));
|
||||
|
||||
if ($previousTokenWasComment) {
|
||||
$output .= ' ' . $text;
|
||||
} else {
|
||||
$output .= '</code>' .
|
||||
'</pre>' .
|
||||
'</div>' .
|
||||
'<div class="doc">' .
|
||||
$text;
|
||||
}
|
||||
|
||||
$previousTokenWasComment = true;
|
||||
} else {
|
||||
if ($previousTokenWasComment) {
|
||||
$output .= '</div>' .
|
||||
'<div class="code">' .
|
||||
'<pre>' .
|
||||
'<code class="language-php">';
|
||||
}
|
||||
|
||||
$output .= htmlspecialchars($text);
|
||||
$previousTokenWasComment = false;
|
||||
}
|
||||
}
|
||||
|
||||
$output .= '</code>' .
|
||||
'</pre>' .
|
||||
'</div>' .
|
||||
'</div>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads config file into a global
|
||||
*
|
||||
|
@ -30,7 +30,9 @@ class BuildTest extends BaseTestCase
|
||||
$html = file_get_contents('/tmp/apprentice_output/test.html');
|
||||
$expectedHtml = "<div>Test Title</div>\n" .
|
||||
"<div>Test Subtitle</div>\n" .
|
||||
"<div>Test Description</div>\n";
|
||||
"<div>Test Description</div>\n" .
|
||||
"<div><p>Test comment</p>\n" .
|
||||
"<pre><code class="language-php">\$test = 'test';</code></pre></div>\n";
|
||||
|
||||
$this->assertFalse(empty($html));
|
||||
$this->assertEquals($expectedHtml, $html);
|
||||
@ -44,7 +46,9 @@ class BuildTest extends BaseTestCase
|
||||
|
||||
$expectedHtml = "<div>Test Title</div>\n" .
|
||||
"<div>Test Subtitle</div>\n" .
|
||||
"<div>Test Description</div>\n";
|
||||
"<div>Test Description</div>\n" .
|
||||
"<div><p>Test comment</p>\n" .
|
||||
"<pre><code class="language-php">\$test = 'test';</code></pre></div>\n";
|
||||
$expectedHtml2 = "<div>index</div>\n";
|
||||
|
||||
$html = file_get_contents('/tmp/apprentice_output/test.html');
|
||||
|
@ -50,17 +50,6 @@ class FunctionsTest extends BaseTestCase
|
||||
$this->assertEquals("<test></test>\n", $icon);
|
||||
}
|
||||
|
||||
public function test_code_table()
|
||||
{
|
||||
$php = file_get_contents(__DIR__ . '/static/code_table.php');
|
||||
$expectedOutput = file_get_contents(__DIR__ . '/static/code_table.html');
|
||||
|
||||
$html = code_table($php);
|
||||
|
||||
$this->assertFalse(empty($html));
|
||||
$this->assertEquals($expectedOutput, $html . "\n");
|
||||
}
|
||||
|
||||
public function test_partial()
|
||||
{
|
||||
partial('partial', ['test' => 'test var']);
|
||||
|
4
test/static/chapter/test.md
Normal file
4
test/static/chapter/test.md
Normal file
@ -0,0 +1,4 @@
|
||||
Test comment
|
||||
```php
|
||||
$test = 'test';
|
||||
```
|
@ -1,4 +0,0 @@
|
||||
<?php
|
||||
|
||||
// test comment
|
||||
$test = 'test';
|
@ -1,4 +0,0 @@
|
||||
<div class="grid-code"><div class="doc"></div><div class="code"><pre><code class="language-php"><?php
|
||||
|
||||
</code></pre></div><div class="doc">this is a test</div><div class="code"><pre><code class="language-php">$test = 'some test code';
|
||||
</code></pre></div></div>
|
@ -1,4 +0,0 @@
|
||||
<?php
|
||||
|
||||
// this is a test
|
||||
$test = 'some test code';
|
@ -4,13 +4,13 @@ use Apprentice\Page;
|
||||
|
||||
return [
|
||||
'icon_dir' => __DIR__ . '/../icons',
|
||||
'code_dir' => __DIR__ . '/code',
|
||||
'chapter_dir' => __DIR__ . '/chapter',
|
||||
'templates_dir' => __DIR__ . '/templates',
|
||||
'output_dir' => '/tmp/apprentice_output',
|
||||
'files_dir' => __DIR__ . '/files',
|
||||
'pages' => [
|
||||
Page::create('index', 'index.phtml'),
|
||||
Page::create('test', null, 'test.php', [
|
||||
Page::create('test', null, 'test.md', [
|
||||
'title' => 'Test Title',
|
||||
'subtitle' => 'Test Subtitle',
|
||||
'description' => 'Test Description',
|
||||
|
@ -1,3 +1,4 @@
|
||||
<div><?= escape($title) ?></div>
|
||||
<div><?= escape($subtitle) ?></div>
|
||||
<div><?= escape($description) ?></div>
|
||||
<div><?= escape($chapter) ?></div>
|
||||
|
Reference in New Issue
Block a user