diff --git a/assets/templates/_table_of_contents.phtml b/assets/templates/_table_of_contents.phtml index 7a240ca..841c855 100644 --- a/assets/templates/_table_of_contents.phtml +++ b/assets/templates/_table_of_contents.phtml @@ -20,6 +20,7 @@
  • Classes: Visibility
  • Classes: Constructor
  • Static
  • +
  • Interfaces
  • Credits diff --git a/code/interfaces.php b/code/interfaces.php new file mode 100644 index 0000000..2498e17 --- /dev/null +++ b/code/interfaces.php @@ -0,0 +1,52 @@ +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); diff --git a/config.php b/config.php index 39cc883..d026988 100644 --- a/config.php +++ b/config.php @@ -135,6 +135,12 @@ return [ 'title' => 'Static', 'subtitle' => 'Class properties and methods', 'previous' => 'classes-constructor', + 'next' => 'interfaces', + ]), + Page::create('interfaces', null, 'interfaces.php', [ + 'title' => 'Interfaces', + 'subtitle' => 'Writing code contracts', + 'previous' => 'static', 'next' => '', ]), ],