1
0
mirror of https://github.com/restoreddev/phpapprentice.git synced 2025-08-28 08:50:15 +02:00

Numbered basics chapters for sortability

This commit is contained in:
Andrew Davis
2019-01-12 15:33:19 -06:00
parent 8aaf7d66fb
commit 81a7371000
19 changed files with 18 additions and 18 deletions

View 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();
```
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
```