mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-07-09 17:46:20 +02:00
Chapter editing fixes
This commit is contained in:
@ -51,7 +51,7 @@ $android->turnOn();
|
||||
$android->unlock();
|
||||
```
|
||||
|
||||
Lastly, you cannot create an instance of an abstract class. PHP would not know how to use the abstract methods
|
||||
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(); // causes an error
|
||||
|
@ -3,7 +3,8 @@ In the previous examples, we always left the parentheses empty.
|
||||
```php
|
||||
<?php
|
||||
|
||||
class Hat {
|
||||
class Hat
|
||||
{
|
||||
public $color;
|
||||
|
||||
public function setColor($color)
|
||||
@ -42,7 +43,7 @@ You can write:
|
||||
$ballcap = new Ballcap('Blue');
|
||||
```
|
||||
|
||||
Constructors do not return values because the return value is a always a new object.
|
||||
Constructors do not return values because the return value is always a new object.
|
||||
```php
|
||||
class Tophat
|
||||
{
|
||||
|
@ -50,7 +50,7 @@ class Motorcycle extends Vehicle
|
||||
}
|
||||
```
|
||||
|
||||
Outputs "driving...".
|
||||
The `pushPedal` method outputs "driving...".
|
||||
```php
|
||||
$cycle = new Motorcycle();
|
||||
$cycle->pushPedal();
|
||||
@ -71,7 +71,7 @@ class Racecar extends Vehicle
|
||||
}
|
||||
```
|
||||
|
||||
Outputs "driving..." and "driving even faster...".
|
||||
The `drive` method on `Racecar` now outputs "driving..." and "driving even faster...".
|
||||
```php
|
||||
$racecar = new Racecar();
|
||||
$racecar->drive();
|
||||
|
@ -25,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 attribute on the instance using `->`.
|
||||
```php
|
||||
$bike = new Bicycle();
|
||||
$bike->color = 'Blue';
|
||||
@ -55,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 with parentheses after the method name.
|
||||
```php
|
||||
$bike = new Tricycle();
|
||||
$bike->color = 'Red';
|
||||
|
@ -10,32 +10,24 @@ $b = false;
|
||||
|
||||
There are many constructs and functions that will return a boolean.
|
||||
To start, let's look at comparisons.
|
||||
|
||||
Double equals checks if two values are equal.
|
||||
```php
|
||||
$one = 1;
|
||||
$two = 2;
|
||||
```
|
||||
|
||||
Double equals checks if two values are equal.
|
||||
This statement will return false.
|
||||
```php
|
||||
$one == $two;
|
||||
$one == $two; // returns false
|
||||
```
|
||||
|
||||
An exclamation point and equal sign check if two values are not equal.
|
||||
This statement will return true.
|
||||
```php
|
||||
$one != $two;
|
||||
$one != $two; // returns true
|
||||
```
|
||||
|
||||
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;
|
||||
$one > $two; // returns false
|
||||
$one < $two; // returns true
|
||||
```
|
||||
|
||||
If you combine a greater than or less than symbol with an equal,
|
||||
|
@ -15,7 +15,7 @@ In the case above, since `$animal` equals 'cow', the statement returns true and
|
||||
|
||||
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.
|
||||
If none of the checks return true, then the `else` block will be executed.
|
||||
```php
|
||||
$animal = 'bird';
|
||||
if ($animal == 'dog') {
|
||||
|
@ -56,7 +56,7 @@ $sum = function ($a, $b) {
|
||||
};
|
||||
```
|
||||
|
||||
You can execute a closure by putting parentheses after the variable.
|
||||
You can execute a closure by putting parentheses after the variable that contains the closure.
|
||||
```php
|
||||
echo $sum(1, 2) . "\n";
|
||||
```
|
||||
|
@ -44,17 +44,19 @@ interface Payment
|
||||
public function charge($amount);
|
||||
}
|
||||
|
||||
class CreditCard
|
||||
class CreditCard implements Payment
|
||||
{
|
||||
public function charge($amount)
|
||||
{
|
||||
|
||||
// contacts a credit card payment provider...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Since `CreditCard` implements `Payment`, other developers can use the charge method, knowing it exists on the class.
|
||||
Since `CreditCard` implements `Payment`, a developer can check that it implements `Payment` and then use the `charge` method knowing the function exists on the class.
|
||||
```php
|
||||
$creditCard = new CreditCard();
|
||||
if ($creditCard instanceof Payment) {
|
||||
$creditCard->charge(25);
|
||||
}
|
||||
```
|
||||
|
@ -53,7 +53,7 @@ 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.
|
||||
the object? A common reason is to make the code more readable.
|
||||
```php
|
||||
class TinyHouse
|
||||
{
|
||||
|
Reference in New Issue
Block a user