mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-08-02 21:07:43 +02:00
Proofreading and code blocking keywords
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
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"
|
||||
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
|
||||
@@ -40,7 +40,7 @@ class Android extends CellPhone
|
||||
}
|
||||
```
|
||||
|
||||
Our iPhone and Android classes can now both use the turnOn method and the unlock method.
|
||||
Our iPhone and Android classes can now both use the `turnOn` method and the `unlock` method.
|
||||
```php
|
||||
$iPhone = new iPhone();
|
||||
$iPhone->turnOn();
|
||||
|
@@ -1,6 +1,6 @@
|
||||
|
||||
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.
|
||||
All PHP files must start with a `<?php` tag unless it is for a html template.
|
||||
(We will learn about html templates later.)
|
||||
|
||||
```php
|
||||
@@ -12,12 +12,14 @@ 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.
|
||||
```php
|
||||
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.
|
||||
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";
|
||||
```
|
||||
|
@@ -8,15 +8,9 @@ If so, it will return true. If not, it will return false.
|
||||
$a = true;
|
||||
$b = true;
|
||||
$c = false;
|
||||
```
|
||||
|
||||
Returns true.
|
||||
```php
|
||||
$a && $b;
|
||||
```
|
||||
Returns false.
|
||||
```php
|
||||
$a && $c;
|
||||
$a && $b; // returns true
|
||||
$a && $c; // returns false
|
||||
```
|
||||
|
||||
Using two pipe characters checks if either value is true.
|
||||
@@ -27,27 +21,14 @@ $a = true;
|
||||
$b = false;
|
||||
$c = false;
|
||||
$d = true;
|
||||
|
||||
$a || $b; // returns true
|
||||
$b || $c; // returns false
|
||||
$a || $d; // returns true
|
||||
```
|
||||
|
||||
Returns true.
|
||||
```php
|
||||
$a || $b;
|
||||
```
|
||||
Returns false.
|
||||
```php
|
||||
$b || $c;
|
||||
```
|
||||
Returns true.
|
||||
```php
|
||||
$a || $d;
|
||||
```
|
||||
|
||||
Using an exclamation point returns the value flipped.
|
||||
Using an exclamation point returns the opposite value.
|
||||
```php
|
||||
$d = true;
|
||||
```
|
||||
|
||||
Outputs false.
|
||||
```php
|
||||
echo !$d;
|
||||
echo !$d; // outputs false
|
||||
```
|
||||
|
@@ -53,7 +53,7 @@ class Tophat
|
||||
}
|
||||
```
|
||||
|
||||
"$tophat" now holds an instance of Tophat, not the color "Grey".
|
||||
`$tophat` now holds an instance of `Tophat`, not the color "Grey".
|
||||
```php
|
||||
$tophat = new Tophat('Grey');
|
||||
```
|
||||
|
@@ -1,5 +1,5 @@
|
||||
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"
|
||||
properties and methods. To make a class a child of another, use the `extends`
|
||||
keyword after the class name.
|
||||
```php
|
||||
<?php
|
||||
@@ -39,7 +39,7 @@ $tractor = new Tractor();
|
||||
$tractor->drive();
|
||||
```
|
||||
|
||||
A class can use a parent's property or method from the "$this" variable.
|
||||
A class can use a parent's property or method from the `$this` variable.
|
||||
```php
|
||||
class Motorcycle extends Vehicle
|
||||
{
|
||||
@@ -56,9 +56,9 @@ $cycle = new Motorcycle();
|
||||
$cycle->pushPedal();
|
||||
```
|
||||
|
||||
If you override a parent's property or method, the "$this" variable will refer to the child's
|
||||
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.
|
||||
use the `parent` keyword.
|
||||
```php
|
||||
class Racecar extends Vehicle
|
||||
{
|
||||
|
@@ -1,5 +1,5 @@
|
||||
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.
|
||||
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
|
||||
@@ -16,7 +16,7 @@ class Phone
|
||||
}
|
||||
```
|
||||
|
||||
We cannot set the number using "$phone->number = '123-456-7890'".
|
||||
We cannot set the number using `$phone->number = '123-456-7890'`.
|
||||
Instead, we can use the public method.
|
||||
```php
|
||||
$phone = new Phone();
|
||||
@@ -39,9 +39,9 @@ class Phone2
|
||||
}
|
||||
```
|
||||
|
||||
The "protected" and "private" keywords work a little differently.
|
||||
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.
|
||||
However, a method or property marked `protected` can still be accessed by a child class.
|
||||
```php
|
||||
class Phone3
|
||||
{
|
||||
@@ -56,8 +56,8 @@ class Phone3
|
||||
}
|
||||
```
|
||||
|
||||
In class "Smartphone", the "caller" property is accessible because the parent class
|
||||
has it marked as "protected". However, "Smartphone" cannot access the "number" property
|
||||
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
|
||||
|
@@ -1,4 +1,3 @@
|
||||
|
||||
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
|
||||
@@ -9,7 +8,7 @@ class Car
|
||||
}
|
||||
```
|
||||
|
||||
To create an instance of a class, you use the "new" keyword in front of the class name
|
||||
To create an instance of a class, you use the `new` keyword in front of the class name
|
||||
with parentheses.
|
||||
```php
|
||||
$car = new Car();
|
||||
@@ -17,7 +16,7 @@ $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.
|
||||
word `public` and a variable name inside the class definition.
|
||||
```php
|
||||
class Bicycle
|
||||
{
|
||||
@@ -26,7 +25,7 @@ class Bicycle
|
||||
```
|
||||
|
||||
Then, when you create an instance of the class, you can set and use
|
||||
the color attribute on the bicycle using "->".
|
||||
the color attribute on the bicycle using `->`.
|
||||
```php
|
||||
$bike = new Bicycle();
|
||||
$bike->color = 'Blue';
|
||||
@@ -42,8 +41,8 @@ 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.
|
||||
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
|
||||
{
|
||||
@@ -56,7 +55,7 @@ class Tricycle
|
||||
}
|
||||
```
|
||||
|
||||
You can execute a method on an object using the same "->" arrow characters.
|
||||
You can execute a method on an object using the same `->` arrow characters.
|
||||
```php
|
||||
$bike = new Tricycle();
|
||||
$bike->color = 'Red';
|
||||
|
@@ -1,4 +1,3 @@
|
||||
|
||||
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.
|
||||
@@ -48,7 +47,7 @@ $one >= $two;
|
||||
You can also check that two values are equal and of the same type
|
||||
by using three equal signs.
|
||||
|
||||
This returns true.
|
||||
The following comparisons return true.
|
||||
```php
|
||||
1 == 1;
|
||||
1 == '1';
|
||||
@@ -57,7 +56,7 @@ This returns true.
|
||||
1 === 1;
|
||||
```
|
||||
|
||||
This returns false.
|
||||
These return false.
|
||||
```php
|
||||
1 === '1';
|
||||
1 === true;
|
||||
|
@@ -1,4 +1,3 @@
|
||||
|
||||
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.
|
||||
@@ -12,10 +11,10 @@ if ($animal == 'cow') {
|
||||
```
|
||||
|
||||
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.
|
||||
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 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';
|
||||
@@ -32,7 +31,7 @@ if ($animal == 'dog') {
|
||||
|
||||
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".
|
||||
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';
|
||||
|
@@ -1,5 +1,5 @@
|
||||
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
|
||||
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
|
||||
@@ -15,7 +15,7 @@ class Processor
|
||||
}
|
||||
```
|
||||
|
||||
In this case, if someone tried to use the Processor class
|
||||
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
|
||||
@@ -26,8 +26,8 @@ $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.
|
||||
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');
|
||||
@@ -37,7 +37,7 @@ try {
|
||||
```
|
||||
|
||||
You can make your own custom exceptions as well. They are just classes
|
||||
that extend Exception.
|
||||
that extend the `Exception` class.
|
||||
```php
|
||||
class MyCustomException extends Exception {}
|
||||
```
|
||||
@@ -51,8 +51,8 @@ try {
|
||||
}
|
||||
```
|
||||
|
||||
Since all exceptions inherit from Exception, catching
|
||||
Exception will catch any and all exceptions that might be thrown.
|
||||
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');
|
||||
|
@@ -27,8 +27,8 @@ function greet($firstname, $lastname) {
|
||||
```
|
||||
|
||||
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.
|
||||
'John' is assigned to `$firstname` and 'Smith' is assigned to
|
||||
`$lastname`.
|
||||
```php
|
||||
greet('John', 'Smith');
|
||||
```
|
||||
|
@@ -1,4 +1,4 @@
|
||||
The word "interface" is a confusing term because it is used for so many different concepts.
|
||||
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.
|
||||
@@ -12,8 +12,8 @@ interface Chair
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
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
|
||||
@@ -53,7 +53,7 @@ class CreditCard
|
||||
}
|
||||
```
|
||||
|
||||
Since CreditCard implements Payment, other developers can use the charge method, knowing it exists on the class.
|
||||
Since `CreditCard` implements `Payment`, other developers can use the charge method, knowing it exists on the class.
|
||||
```php
|
||||
$creditCard = new CreditCard();
|
||||
$creditCard->charge(25);
|
||||
|
@@ -1,6 +1,6 @@
|
||||
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.
|
||||
A `while` loop will continue to run the block of code as long as the value in parentheses is true.
|
||||
```php
|
||||
<?php
|
||||
|
||||
@@ -11,9 +11,9 @@ while ($num > 0) {
|
||||
}
|
||||
```
|
||||
|
||||
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
|
||||
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;
|
||||
@@ -23,7 +23,7 @@ do {
|
||||
} while ($num < 5);
|
||||
```
|
||||
|
||||
"for" loops allow you to create a more concise while loop.
|
||||
`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.
|
||||
@@ -33,9 +33,9 @@ for ($i = 0; $i < 10; $i++) {
|
||||
}
|
||||
```
|
||||
|
||||
A "foreach" loop allows you to easily loop over an array.
|
||||
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
|
||||
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];
|
||||
@@ -44,7 +44,7 @@ foreach ($set as $num) {
|
||||
}
|
||||
```
|
||||
|
||||
In loops, you can use the keyword "break" to stop the loop execution
|
||||
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'];
|
||||
@@ -56,7 +56,7 @@ foreach ($values as $value) {
|
||||
}
|
||||
```
|
||||
|
||||
The "continue" keyword stops executing the current loop iteration,
|
||||
The `continue` keyword stops executing the current loop iteration,
|
||||
but then allows the loop to continue with other iterations.
|
||||
```php
|
||||
$values = ['one', 'skip', 'three'];
|
||||
|
@@ -22,7 +22,7 @@ $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
|
||||
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
|
||||
@@ -42,7 +42,7 @@ class Skyscraper
|
||||
}
|
||||
```
|
||||
|
||||
You can access static methods and properties using double colons on "self" inside the object
|
||||
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
|
||||
@@ -75,7 +75,7 @@ class TinyHouse
|
||||
}
|
||||
```
|
||||
|
||||
Using "build" can make more sense than "new", but it is ultimately a personal preference.
|
||||
Using `build` can make more sense than `new`, but it is ultimately a personal preference.
|
||||
```php
|
||||
$house = TinyHouse::build('Blue', 4, true);
|
||||
```
|
||||
|
@@ -8,7 +8,7 @@ $lastname = "Johnson";
|
||||
```
|
||||
|
||||
A double quoted string can interpret special characters starting
|
||||
with a back slash to create formatting. The \n creates a newline
|
||||
with a back slash to create formatting. The `\n` creates a newline
|
||||
between the names and after them.
|
||||
```php
|
||||
echo "Jacob\nJones\n";
|
||||
|
@@ -1,4 +1,3 @@
|
||||
|
||||
The variable is the basic building block of any programming language.
|
||||
In PHP, all variables start with a dollar sign.
|
||||
```php
|
||||
|
Reference in New Issue
Block a user