1
0
mirror of https://github.com/restoreddev/phpapprentice.git synced 2025-07-14 12:06:19 +02:00

Added page for interfaces

This commit is contained in:
Andrew Davis
2018-09-16 12:42:50 -05:00
parent 60f799bc55
commit 97efca915c
3 changed files with 59 additions and 0 deletions

View File

@ -20,6 +20,7 @@
<li><a href="<?= page_path('classes-visibility') ?>">Classes: Visibility</a></li> <li><a href="<?= page_path('classes-visibility') ?>">Classes: Visibility</a></li>
<li><a href="<?= page_path('classes-constructor') ?>">Classes: Constructor</a></li> <li><a href="<?= page_path('classes-constructor') ?>">Classes: Constructor</a></li>
<li><a href="<?= page_path('static') ?>">Static</a></li> <li><a href="<?= page_path('static') ?>">Static</a></li>
<li><a href="<?= page_path('interfaces') ?>">Interfaces</a></li>
</ol> </ol>
<a href="<?= page_path('credits') ?>">Credits</a> <a href="<?= page_path('credits') ?>">Credits</a>
</div> </div>

52
code/interfaces.php Normal file
View File

@ -0,0 +1,52 @@
<?php
// 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.
interface Chair
{
public function setColor($color);
public function setLegs($number);
}
// 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.
class Recliner implements Chair
{
private $color;
private $legs;
public function setColor($color)
{
$this->color = $color;
}
public function setLegs($number)
{
$this->legs = $number;
}
}
// Interfaces are helpful when you are using code created by someone else.
// For example, another developer may have created code that manages online payments, but they want to give you
// the ability to create your own payment class that works with their code. In that case, the developer
// creates an interface with all the required methods they need to charge a payment. The interface
// becomes a contract between your code and the other developer's code to work a certain way.
interface Payment
{
public function charge($amount);
}
class CreditCard
{
public function charge($amount)
{
}
}
// Since CreditCard implements Payment, other developers can use the charge method, knowing it exists on the class.
$creditCard = new CreditCard();
$creditCard->charge(25);

View File

@ -135,6 +135,12 @@ return [
'title' => 'Static', 'title' => 'Static',
'subtitle' => 'Class properties and methods', 'subtitle' => 'Class properties and methods',
'previous' => 'classes-constructor', 'previous' => 'classes-constructor',
'next' => 'interfaces',
]),
Page::create('interfaces', null, 'interfaces.php', [
'title' => 'Interfaces',
'subtitle' => 'Writing code contracts',
'previous' => 'static',
'next' => '', 'next' => '',
]), ]),
], ],