Classes and OOP (Part 4)

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Classes and OOP (Part 4)



1.0 Previous Tutorial


This is the fourth tutorial in a series of tutorials on classes and OOP.

First Tutorial
Second Tutorial
Third Tutorial

In the previous tutorial we learned how to extend a class with either one child or with multiple. We also learned how we could dynamically use a variable to call on a certain class (doesn't necessarily need to be extending another class to call on a class this way).

1.1 Tutorial Introduction


In this tutorial, we would continue with extending classes but with a twist. Instead of simply extending a parent class, we are going to use an interface and implement it with a class.

As a reminder, in the previous tutorial we have left off with the following class. And instead of adding to it in this tutorial, we are going to change it up (you might want to save a back-up of this file for later if you are following along with the tutorial).
PHP Code: [ Select ]
<?php
class fruit
{
 
    private $amount = 2;
   
    public function __construct()
    {
        echo "Picking up a(n) {$this->fruit}.<br />\n";
    }
 
    public function eat()
    {
        $this->prepare();
        $this->chew();
        $this->swallow();
    }
   
    public function amount($number)
    {
        if($number < 11)
        {
            $this->amount = $number;
        }
    }
   
    public function __destruct()
    {
        echo "We ate a total of {$this->amount} {$this->fruit}(s).<br />\n";
    }
   
}
 
class apple extends fruit
{
 
    protected $fruit = 'apple';
 
    protected function prepare()
    {
        echo "Washing the apple.<br />";
    }
   
    protected function chew()
    {
        echo "Biting the apple.<br />";
        echo "Chewing the apple.<br />";
    }
   
    protected function swallow()
    {
        echo "Swallowing the apple.<br />";
    }
 
}
 
class banana extends fruit
{
 
    protected $fruit = 'banana';
 
    protected function prepare()
    {
        echo "Peeling the banana.<br />";
    }
   
    protected function chew()
    {
        echo "Biting the banana.<br />";
        echo "Chewing the banana.<br />";
    }
   
    protected function swallow()
    {
        echo "Swallowing the banana.<br />";
    }
 
}
 
$fruit = 'banana';
 
$product = new $fruit();
 
$product->eat();
?>
  1. <?php
  2. class fruit
  3. {
  4.  
  5.     private $amount = 2;
  6.    
  7.     public function __construct()
  8.     {
  9.         echo "Picking up a(n) {$this->fruit}.<br />\n";
  10.     }
  11.  
  12.     public function eat()
  13.     {
  14.         $this->prepare();
  15.         $this->chew();
  16.         $this->swallow();
  17.     }
  18.    
  19.     public function amount($number)
  20.     {
  21.         if($number < 11)
  22.         {
  23.             $this->amount = $number;
  24.         }
  25.     }
  26.    
  27.     public function __destruct()
  28.     {
  29.         echo "We ate a total of {$this->amount} {$this->fruit}(s).<br />\n";
  30.     }
  31.    
  32. }
  33.  
  34. class apple extends fruit
  35. {
  36.  
  37.     protected $fruit = 'apple';
  38.  
  39.     protected function prepare()
  40.     {
  41.         echo "Washing the apple.<br />";
  42.     }
  43.    
  44.     protected function chew()
  45.     {
  46.         echo "Biting the apple.<br />";
  47.         echo "Chewing the apple.<br />";
  48.     }
  49.    
  50.     protected function swallow()
  51.     {
  52.         echo "Swallowing the apple.<br />";
  53.     }
  54.  
  55. }
  56.  
  57. class banana extends fruit
  58. {
  59.  
  60.     protected $fruit = 'banana';
  61.  
  62.     protected function prepare()
  63.     {
  64.         echo "Peeling the banana.<br />";
  65.     }
  66.    
  67.     protected function chew()
  68.     {
  69.         echo "Biting the banana.<br />";
  70.         echo "Chewing the banana.<br />";
  71.     }
  72.    
  73.     protected function swallow()
  74.     {
  75.         echo "Swallowing the banana.<br />";
  76.     }
  77.  
  78. }
  79.  
  80. $fruit = 'banana';
  81.  
  82. $product = new $fruit();
  83.  
  84. $product->eat();
  85. ?>


2.0 Interfaces


An interface is pretty simple actually, and it does a lot of good when it comes to creating an expandable system (e.g: a system for modules... since you would be adding different modules).

Let me go a bit more into detail here to help you understand interfaces and the use of them before I show you how to create the interfaces and how to implement it within a class.

An interface is a point of interaction between classes. It's like a guideline of how classes that would implement it should look like. Implementing an interface is creating a contract between the interface and the class saying that the class will have all of the methods mentioned in the interface and extend on them.

An interface is not the same as a class and has a different role in OOP. It also does not behave like a class and is not used as a class. You can't initiate an interface like you would a class.

Interface is about keeping consistency within all of the classes that implement it, which makes adding classes to the system much easier to use (especially if they are called dynamically).

For instance you have a system which selects a module and runs the selected module. In order for you to use one code to run multiple modules, you need the modules to be created under guidelines which makes it possible to do so. This enables you to just add a class for that module and an option to the form (or a link, or however else you want to dynamically call on that class) and use that module without writing a whole lot more code to enable you to use that class.

Here is how an interface looks like.
PHP Code: [ Select ]
<?php
interface fruit
{
 
    function __construct();
   
    function eat();
   
    function amount($number);
   
    function __destruct();
 
}
  1. <?php
  2. interface fruit
  3. {
  4.  
  5.     function __construct();
  6.    
  7.     function eat();
  8.    
  9.     function amount($number);
  10.    
  11.     function __destruct();
  12.  
  13. }

Looks weird, doesn't it? It doesn't define the methods or anything, what it does it tell the classes that implement it that they need to have those methods defined. Since it's an interface all of the methods in the interface are public. They cannot be protected or private, which means that in this case, all those functions that used to be either protected or private (in order to affect the usage of the class) are going to be defined in the class implementing the interface, but not set-up in the interface.

Here is how we would implement an interface.
PHP Code: [ Select ]
class apple implements fruit
{
 
}
  1. class apple implements fruit
  2. {
  3.  
  4. }

It's the same as extends (previous tutorial) except you use the keyword implements instead of extends. Here is how the apple class implementing the fruit interface would look like.
PHP Code: [ Select ]
class apple implements fruit
{
   
    private $amount = 2;
   
    function __construct()
    {
        echo "Picking up an apple.<br />\n";
    }
 
    public function eat()
    {
        $this->prepare();
        $this->chew();
        $this->swallow();
    }
   
    public function amount($number, $size = 3)
    {
        if($number < 11)
        {
            $this->amount = $number;
        }
    }
   
    private function prepare()
    {
        echo "Washing the apple.<br />";
    }
   
    private function chew()
    {
        echo "Biting the apple.<br />";
        echo "Chewing the apple.<br />";
    }
   
    private function swallow()
    {
        echo "Swallowing the apple.<br />";
    }
 
    function __destruct()
    {
        echo "We ate a total of {$this->amount} apple(s).<br />\n";
    }
   
}
  1. class apple implements fruit
  2. {
  3.    
  4.     private $amount = 2;
  5.    
  6.     function __construct()
  7.     {
  8.         echo "Picking up an apple.<br />\n";
  9.     }
  10.  
  11.     public function eat()
  12.     {
  13.         $this->prepare();
  14.         $this->chew();
  15.         $this->swallow();
  16.     }
  17.    
  18.     public function amount($number, $size = 3)
  19.     {
  20.         if($number < 11)
  21.         {
  22.             $this->amount = $number;
  23.         }
  24.     }
  25.    
  26.     private function prepare()
  27.     {
  28.         echo "Washing the apple.<br />";
  29.     }
  30.    
  31.     private function chew()
  32.     {
  33.         echo "Biting the apple.<br />";
  34.         echo "Chewing the apple.<br />";
  35.     }
  36.    
  37.     private function swallow()
  38.     {
  39.         echo "Swallowing the apple.<br />";
  40.     }
  41.  
  42.     function __destruct()
  43.     {
  44.         echo "We ate a total of {$this->amount} apple(s).<br />\n";
  45.     }
  46.    
  47. }

And if we run that.
PHP Code: [ Select ]
$product = new apple();
 
$product->eat();
  1. $product = new apple();
  2.  
  3. $product->eat();

We would get.
Quote:
Picking up an apple.
Washing the apple.
Biting the apple.
Chewing the apple.
Swallowing the apple.
We ate a total of 2 apple(s).

Here, you would notice that all of the functions that used to be defined in a parent class (in the previous tutorial) have being actually moved to the class implementing the interface. In the actual interface, they are just named and set-up, butthey are actually defined in the class.

Also take note of the function amount, mainly the fact that it takes an argument $number. If you would look in the interface, you would notice that the named and set-up function amount also has that argument in there. For every function named in the interface that has a required argument, that argument also needs to be present in the interface as well. Let's look at the following code.
PHP Code: [ Select ]
public function amount($number, $size)
{
    if($number < 11)
    {
        $this->amount = $number;
    }
}
  1. public function amount($number, $size)
  2. {
  3.     if($number < 11)
  4.     {
  5.         $this->amount = $number;
  6.     }
  7. }

If we have this function in the class with the interface the way it is (with only 1 argument named) we would get an error because the variable $size is required (doesn't have a default value set). But if we make it optional than it is ok if the optional argument is not named in the interface (the following example is okay with the way our interface is right now).
PHP Code: [ Select ]
public function amount($number, $size = 3)
{
    if($number < 11)
    {
        $this->amount = $number;
    }
}
  1. public function amount($number, $size = 3)
  2. {
  3.     if($number < 11)
  4.     {
  5.         $this->amount = $number;
  6.     }
  7. }

Since $size has a default value set (making it optional) then it's okay when e have only 1 argument ($number) named in the interface. It would also be okay to name the argument $size in the interface and have it be optional in the class. (First set of code is the function named in the interface and the second set of code is how the function is implemented in the class).
PHP Code: [ Select ]
function amount($number, $size);

PHP Code: [ Select ]
function amount($number, $size = 3)
{
    if($number < 11)
    {
        $this->amount = $number;
    }
}
  1. function amount($number, $size = 3)
  2. {
  3.     if($number < 11)
  4.     {
  5.         $this->amount = $number;
  6.     }
  7. }

It is also ok if the argument names named in the interface are not the same as in the class implementing the interface.
PHP Code: [ Select ]
function amount($number, $size);

PHP Code: [ Select ]
function amount($number, $shape = 'round')
{
    if($number < 11)
    {
        $this->amount = $number;
    }
}
  1. function amount($number, $shape = 'round')
  2. {
  3.     if($number < 11)
  4.     {
  5.         $this->amount = $number;
  6.     }
  7. }


If we have a second class (banana for instance) we would have all those same functions found in 'apple' in 'orange'... especially '__construct()', 'eat()', 'amount()' and '__destruct()'.
PHP Code: [ Select ]
class banana implements fruit
{
   
    private $amount = 2;
   
    function __construct()
    {
        echo "Picking up a banana.<br />\n";
    }
 
    public function eat()
    {
        $this->prepare();
        $this->chew();
        $this->swallow();
    }
   
    public function amount($number, $size = 3)
    {
        if($number < 11)
        {
            $this->amount = $number;
        }
    }
   
    private function prepare()
    {
        echo "Peeling the banana.<br />";
    }
   
    private function chew()
    {
        echo "Biting the banana.<br />";
        echo "Chewing the banana.<br />";
    }
   
    private function swallow()
    {
        echo "Swallowing the banana.<br />";
    }
 
    function __destruct()
    {
        echo "We ate a total of {$this->amount} banana(s).<br />\n";
    }
 
}
  1. class banana implements fruit
  2. {
  3.    
  4.     private $amount = 2;
  5.    
  6.     function __construct()
  7.     {
  8.         echo "Picking up a banana.<br />\n";
  9.     }
  10.  
  11.     public function eat()
  12.     {
  13.         $this->prepare();
  14.         $this->chew();
  15.         $this->swallow();
  16.     }
  17.    
  18.     public function amount($number, $size = 3)
  19.     {
  20.         if($number < 11)
  21.         {
  22.             $this->amount = $number;
  23.         }
  24.     }
  25.    
  26.     private function prepare()
  27.     {
  28.         echo "Peeling the banana.<br />";
  29.     }
  30.    
  31.     private function chew()
  32.     {
  33.         echo "Biting the banana.<br />";
  34.         echo "Chewing the banana.<br />";
  35.     }
  36.    
  37.     private function swallow()
  38.     {
  39.         echo "Swallowing the banana.<br />";
  40.     }
  41.  
  42.     function __destruct()
  43.     {
  44.         echo "We ate a total of {$this->amount} banana(s).<br />\n";
  45.     }
  46.  
  47. }

Which would display.
Quote:
Picking up a banana.
Peeling the banana.
Biting the banana.
Chewing the banana.
Swallowing the banana.
We ate a total of 2 banana(s).


2.1 Extending Interfaces


We could extend in interface with a different interface if we wanted/needed to. For instance if we had an interface 'food' and we were extending that interface with 'fruit' so we would affect the way that our classes were written, we would do it the following way.
PHP Code: [ Select ]
interface food
{
 
    function eat();
   
    function prepare();
   
    function chew();
   
    function swallow();
 
}
 
interface fruit extends food
{
 
    function __construct();
   
    function amount($number);
   
    function __destruct();
 
}
  1. interface food
  2. {
  3.  
  4.     function eat();
  5.    
  6.     function prepare();
  7.    
  8.     function chew();
  9.    
  10.     function swallow();
  11.  
  12. }
  13.  
  14. interface fruit extends food
  15. {
  16.  
  17.     function __construct();
  18.    
  19.     function amount($number);
  20.    
  21.     function __destruct();
  22.  
  23. }

So we named the methods in food that we use in our main class (which are private) so now we need to change those functions in those classes ('apple' and 'banana') to be public instead of private. (Note, we also moved the location of function 'eat()' from interface 'fruit' to interface 'food'. Doesn't really matter.

2.2 Multiple Interfaces


If we ever want/need to, we could extend multiple interfaces by making a comma delimited list of all of the interfaces we are extending. For example (not adding with our tutorial example code).
PHP Code: [ Select ]
interface apple extends food, product, item
{
 
    // ...
 
}
  1. interface apple extends food, product, item
  2. {
  3.  
  4.     // ...
  5.  
  6. }

On top of this, a class implementing an interface can also implement multiple interfaces (instead of an interface extending an interface).
PHP Code: [ Select ]
class apple implements food, product, item
{
 
}
  1. class apple implements food, product, item
  2. {
  3.  
  4. }


2.3 Constants


Yes, we can have constants in our class. You would define them in the beginning of the interface (or class if you are just using a class without interfaces) and call on them as you would a static variable (without the dollar sign). For example (not used in our tutorial example code).
PHP Code: [ Select ]
interface car
{
    const type = 'Hatchback';
}
  1. interface car
  2. {
  3.     const type = 'Hatchback';
  4. }

And if you ever use a constant, you would call on it as.
PHP Code: [ Select ]
echo car::type; // Would print 'Hatchback' (Without the quotes)

The difference between functions named in the interfaces and constants is that the constants are already defined in the interface, and you can't over-ride it in the class. The following class implementing the interface 'car' would throw you an error (as it is over-riding a constant).
PHP Code: [ Select ]
class focus implements car
{
    const type = 'Coupe';
}
  1. class focus implements car
  2. {
  3.     const type = 'Coupe';
  4. }

It would fail even if it is set to the same thing (If both interface constant and class constant of the same name are set to the same value).

3.0 Our File


At the end of this tutorial, our file should look like:
PHP Code: [ Select ]
<?php
interface food
{
 
    function eat();
   
    function prepare();
   
    function chew();
   
    function swallow();
 
}
 
interface fruit extends food
{
 
    function __construct();
   
    function amount($number);
   
    function __destruct();
 
}
 
class apple implements fruit
{
   
    private $amount = 2;
   
    function __construct()
    {
        echo "Picking up an apple.<br />\n";
    }
 
    public function eat()
    {
        $this->prepare();
        $this->chew();
        $this->swallow();
    }
   
    public function amount($number, $size = 3)
    {
        if($number < 11)
        {
            $this->amount = $number;
        }
    }
   
    public function prepare()
    {
        echo "Washing the apple.<br />";
    }
   
    public function chew()
    {
        echo "Biting the apple.<br />";
        echo "Chewing the apple.<br />";
    }
   
    public function swallow()
    {
        echo "Swallowing the apple.<br />";
    }
 
    function __destruct()
    {
        echo "We ate a total of {$this->amount} apple(s).<br />\n";
    }
   
}
 
class banana implements fruit
{
   
    private $amount = 2;
   
    function __construct()
    {
        echo "Picking up a banana.<br />\n";
    }
 
    public function eat()
    {
        $this->prepare();
        $this->chew();
        $this->swallow();
    }
   
    public function amount($number, $size = 3)
    {
        if($number < 11)
        {
            $this->amount = $number;
        }
    }
   
    public function prepare()
    {
        echo "Peeling the banana.<br />";
    }
   
    public function chew()
    {
        echo "Biting the banana.<br />";
        echo "Chewing the banana.<br />";
    }
   
    public function swallow()
    {
        echo "Swallowing the banana.<br />";
    }
 
    function __destruct()
    {
        echo "We ate a total of {$this->amount} banana(s).<br />\n";
    }
 
}
 
$product = new banana();
 
$product->eat();
?>
  1. <?php
  2. interface food
  3. {
  4.  
  5.     function eat();
  6.    
  7.     function prepare();
  8.    
  9.     function chew();
  10.    
  11.     function swallow();
  12.  
  13. }
  14.  
  15. interface fruit extends food
  16. {
  17.  
  18.     function __construct();
  19.    
  20.     function amount($number);
  21.    
  22.     function __destruct();
  23.  
  24. }
  25.  
  26. class apple implements fruit
  27. {
  28.    
  29.     private $amount = 2;
  30.    
  31.     function __construct()
  32.     {
  33.         echo "Picking up an apple.<br />\n";
  34.     }
  35.  
  36.     public function eat()
  37.     {
  38.         $this->prepare();
  39.         $this->chew();
  40.         $this->swallow();
  41.     }
  42.    
  43.     public function amount($number, $size = 3)
  44.     {
  45.         if($number < 11)
  46.         {
  47.             $this->amount = $number;
  48.         }
  49.     }
  50.    
  51.     public function prepare()
  52.     {
  53.         echo "Washing the apple.<br />";
  54.     }
  55.    
  56.     public function chew()
  57.     {
  58.         echo "Biting the apple.<br />";
  59.         echo "Chewing the apple.<br />";
  60.     }
  61.    
  62.     public function swallow()
  63.     {
  64.         echo "Swallowing the apple.<br />";
  65.     }
  66.  
  67.     function __destruct()
  68.     {
  69.         echo "We ate a total of {$this->amount} apple(s).<br />\n";
  70.     }
  71.    
  72. }
  73.  
  74. class banana implements fruit
  75. {
  76.    
  77.     private $amount = 2;
  78.    
  79.     function __construct()
  80.     {
  81.         echo "Picking up a banana.<br />\n";
  82.     }
  83.  
  84.     public function eat()
  85.     {
  86.         $this->prepare();
  87.         $this->chew();
  88.         $this->swallow();
  89.     }
  90.    
  91.     public function amount($number, $size = 3)
  92.     {
  93.         if($number < 11)
  94.         {
  95.             $this->amount = $number;
  96.         }
  97.     }
  98.    
  99.     public function prepare()
  100.     {
  101.         echo "Peeling the banana.<br />";
  102.     }
  103.    
  104.     public function chew()
  105.     {
  106.         echo "Biting the banana.<br />";
  107.         echo "Chewing the banana.<br />";
  108.     }
  109.    
  110.     public function swallow()
  111.     {
  112.         echo "Swallowing the banana.<br />";
  113.     }
  114.  
  115.     function __destruct()
  116.     {
  117.         echo "We ate a total of {$this->amount} banana(s).<br />\n";
  118.     }
  119.  
  120. }
  121.  
  122. $product = new banana();
  123.  
  124. $product->eat();
  125. ?>


3.1 Conclusion


In this tutorial we learned how to create an interface and how to implement it.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 2 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.