mirror of
https://github.com/restoreddev/phpapprentice.git
synced 2025-07-09 17:46:20 +02:00
More editing of chapters
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
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.
|
||||
keyword followed by `class` and the name of the class.
|
||||
```php
|
||||
<?php
|
||||
|
||||
@ -54,5 +54,5 @@ $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();
|
||||
$cellPhone = new CellPhone(); // causes an error
|
||||
```
|
||||
|
@ -1,7 +1,7 @@
|
||||
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
|
||||
First, you can use the `array` construct to pass in values separated by commas
|
||||
and it will return an array.
|
||||
```php
|
||||
<?php
|
||||
@ -25,7 +25,7 @@ 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
|
||||
It is very common to create an array with string keys. The feature
|
||||
is called an associative array or a map.
|
||||
```php
|
||||
$car = ['make' => 'Toyota', 'model' => 'Camry'];
|
||||
|
@ -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 string "Grey".
|
||||
```php
|
||||
$tophat = new Tophat('Grey');
|
||||
```
|
||||
|
@ -15,7 +15,7 @@ class Vehicle
|
||||
class Truck extends Vehicle {}
|
||||
```
|
||||
|
||||
The `Truck` class does not error because `Truck` extends `Vehicle`.
|
||||
Using the `drive` method on the `Truck` class does not cause an error because `Truck` extends `Vehicle`.
|
||||
```php
|
||||
$truck = new Truck();
|
||||
$truck->drive();
|
||||
|
@ -17,7 +17,7 @@ class Processor
|
||||
|
||||
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.
|
||||
`Exception` will be thrown which stops the rest of the code from running.
|
||||
```php
|
||||
$processor = new Processor();
|
||||
$processor->charge('1234');
|
||||
@ -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 braces
|
||||
followed by `catch`, the exception type in parentheses and more braces.
|
||||
```php
|
||||
try {
|
||||
$processor->charge('1234');
|
||||
@ -47,7 +47,7 @@ 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";
|
||||
echo "Caught MyCustomException\n";
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -13,7 +13,7 @@ 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.
|
||||
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
|
||||
|
Reference in New Issue
Block a user