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();
|
$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.
|
so when you try to create an abstract instance you will get an error.
|
||||||
```php
|
```php
|
||||||
$cellPhone = new CellPhone(); // causes an error
|
$cellPhone = new CellPhone(); // causes an error
|
||||||
|
@ -3,7 +3,8 @@ In the previous examples, we always left the parentheses empty.
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
class Hat {
|
class Hat
|
||||||
|
{
|
||||||
public $color;
|
public $color;
|
||||||
|
|
||||||
public function setColor($color)
|
public function setColor($color)
|
||||||
@ -42,7 +43,7 @@ You can write:
|
|||||||
$ballcap = new Ballcap('Blue');
|
$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
|
```php
|
||||||
class Tophat
|
class Tophat
|
||||||
{
|
{
|
||||||
|
@ -50,7 +50,7 @@ class Motorcycle extends Vehicle
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Outputs "driving...".
|
The `pushPedal` method outputs "driving...".
|
||||||
```php
|
```php
|
||||||
$cycle = new Motorcycle();
|
$cycle = new Motorcycle();
|
||||||
$cycle->pushPedal();
|
$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
|
```php
|
||||||
$racecar = new Racecar();
|
$racecar = new Racecar();
|
||||||
$racecar->drive();
|
$racecar->drive();
|
||||||
|
@ -25,7 +25,7 @@ class Bicycle
|
|||||||
```
|
```
|
||||||
|
|
||||||
Then, when you create an instance of the class, you can set and use
|
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
|
```php
|
||||||
$bike = new Bicycle();
|
$bike = new Bicycle();
|
||||||
$bike->color = 'Blue';
|
$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
|
```php
|
||||||
$bike = new Tricycle();
|
$bike = new Tricycle();
|
||||||
$bike->color = 'Red';
|
$bike->color = 'Red';
|
||||||
|
@ -10,32 +10,24 @@ $b = false;
|
|||||||
|
|
||||||
There are many constructs and functions that will return a boolean.
|
There are many constructs and functions that will return a boolean.
|
||||||
To start, let's look at comparisons.
|
To start, let's look at comparisons.
|
||||||
|
|
||||||
|
Double equals checks if two values are equal.
|
||||||
```php
|
```php
|
||||||
$one = 1;
|
$one = 1;
|
||||||
$two = 2;
|
$two = 2;
|
||||||
```
|
|
||||||
|
|
||||||
Double equals checks if two values are equal.
|
$one == $two; // returns false
|
||||||
This statement will return false.
|
|
||||||
```php
|
|
||||||
$one == $two;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
An exclamation point and equal sign check if two values are not equal.
|
An exclamation point and equal sign check if two values are not equal.
|
||||||
This statement will return true.
|
|
||||||
```php
|
```php
|
||||||
$one != $two;
|
$one != $two; // returns true
|
||||||
```
|
```
|
||||||
|
|
||||||
You can use greater than and less than symbols to check for comparisons too.
|
You can use greater than and less than symbols to check for comparisons too.
|
||||||
This statement will return false.
|
|
||||||
```php
|
```php
|
||||||
$one > $two;
|
$one > $two; // returns false
|
||||||
```
|
$one < $two; // returns true
|
||||||
|
|
||||||
This statement will return true.
|
|
||||||
```php
|
|
||||||
$one < $two;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
If you combine a greater than or less than symbol with an equal,
|
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.
|
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.
|
If none of the checks return true, then the `else` block will be executed.
|
||||||
```php
|
```php
|
||||||
$animal = 'bird';
|
$animal = 'bird';
|
||||||
if ($animal == 'dog') {
|
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
|
```php
|
||||||
echo $sum(1, 2) . "\n";
|
echo $sum(1, 2) . "\n";
|
||||||
```
|
```
|
||||||
|
@ -44,17 +44,19 @@ interface Payment
|
|||||||
public function charge($amount);
|
public function charge($amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
class CreditCard
|
class CreditCard implements Payment
|
||||||
{
|
{
|
||||||
public function charge($amount)
|
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
|
```php
|
||||||
$creditCard = new CreditCard();
|
$creditCard = new CreditCard();
|
||||||
$creditCard->charge(25);
|
if ($creditCard instanceof Payment) {
|
||||||
|
$creditCard->charge(25);
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
@ -53,7 +53,7 @@ echo $skyscraper->color . "\n";
|
|||||||
|
|
||||||
Often, you will see static constructors in PHP.
|
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
|
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
|
```php
|
||||||
class TinyHouse
|
class TinyHouse
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user