Object-oriented Programming In PHP – Part II

Yesterday I created a post on OOP and had to split it in two to avoid making it too long. Today, I am going to finish up the remaining part before starting other stuff! So, let us get started here.

object-oriented-programming

Controlling Visibility Through Inheritance With private and protected

While writing code using OOP, you can use private and protected visibility modifiers to control what gets inherited. Attributes or methods declared as private will not be inherited. If an attribute or a method is marked as protected, it will not be visible outside the class (like private) but will be inherited. Example:

<?php
   class A
   {
     private function operation1(){
       echo 'operation1 was called';
     }
     protected function operation2(){
       echo 'operation2 was called';
     }
     public function operation3(){
       echo 'operation3 was called';
     }
   }

   class B extends A
   {
     function __construct(){
       $this->operation1();
       $this->operation2();
       $this->operation3();
     }
   }

   $b = new B();

   #----------------------------------------------------#
   |The above code creates a class A with three methods |
   |with private, protected and public accessibility    |
   |As I mentioned earlier, private attributes and      |
   |methods are not inherited - that means that operat- |
   |ion1() will result in an error when you call class B|
   |constructor                                         |

   |Fatal error: Call to private method A::operation1() |
   |from context 'B'                                    |
   #----------------------------------------------------#

   #The protected operation2() can only be used inside  #
   #the child class but not outside. That being said,   #

   $b->operation2();

   #will result in an error!
   #public operation3() however will not result in any  #
   #error
   $b->operation3();

?>

Overriding

We have shown that a subclass can declare new attributes and methods. It is also valid and sometimes useful to re-declare the same attributes and operations. You might do it to give an attribute in the subclass a different default value to the same attribute in its superclass or to give an operation in the subclass a different functionality to the same operation in its superclass. This is called overriding.

Consider:

<?php
   class A
   {
     public $attribute = "default value";
     function operation(){
       echo "Something <br />";
       echo "The value of \$attribute is ".$this->attribute."<br />";
     }
   }

   #Now if you want to alter the default value of $attribute
   #and provide new functionality to operation() you can create
   #the class B that inherits from A like this:

   class B extends A
   {
     public $attribute = "different value";
     function operation(){
       echo "Something else<br />";
       echo "The value of \$attribute is ".$this->attribute."<br />";
     }
   }

   #Declaring B does not affect the original declaration of A
   #Now see what happens here:

   $a = new A();
   $a->operation();
   #----------------------RESULT----------------------#
   #Something
   #The value of $attribute is default value
   #---------------------Now create one for B---------#

   $b = new B();
   $b->operation();
   #---------------------RESULT-----------------------#
   #Something else
   #The value of $attribute is different value

   #NOTE: overriding attributes and operations in a
   #subclass does not
   #affect the superclass

   #The parent keyword allows you to call the original version
   #of the operation in the parent class. To call A::operation()
   #in class B, you simply do this:
   parent::operation();
?>

Preventing Inheritance and Overriding with final

In PHP, using the keyword final in front of a function prevents it from being overridden in any subclasses. For instance, we could add it to our operation in our class A like this:

<?php
   class A
   {
     public $attribute = "default value";
     final function operation(){
       echo "Something <br />";
       echo "The value of \$attribute is ".$this->attribute."<br />";
     }
   }
   #-------------------------------------------------------#
   #using final prevents you from overriding operation in class
   #B and an attempt to do so will result in a fatal error!
   #Fatal error: Cannot override final method A::operation()

   #You can also use final to prevent a class from being
   #subclassed like so:

   final class A
   {
     //--------------
   }
   #Trying to inherit from A now will result in an error message:
   #Fatal error: Class B may not inherit from final class (A)
?>

Understanding Multiple Inheritance

PHP does not support multiple inheritance – that means each class can only inherit from one parent. There is however no restriction on how many children can share the same parent.

Implementing Interfaces

Since PHP does not support multiple inheritance, you use interfaces to solve problems that would otherwise require multiple inheritance. The idea of an interface is that it specifies a set of functions that must be implemented in classes that implement the particular interface.

Consider a situation where you have a set of classes that you want them to be able to display themselves. Instead of having a parent class with a display() method that they all inherit and override, you can simply implement an interface as follows:

<?php
   interface Displayable
   {
      function display();
   }

   class webPage implements Displayable
   {
     function display(){
       //do some display work here for web page
     }
   }

   #Failure to implement the display() function will result
   #in a fatal error. It is also worth mentioning that
   #a class can inherit from one class and implement more than
   #one interface!
?>

Designing Classes

When I was working on my simple shopping cart, I used some include() or require() to maintain consistency throughout the pages. Now that we know what classes and functions do, we should be able to generate our web pages dynamically!

That will be really fun and that is why I want to save it for tomorrow to avoid diluting it with what I have already done today! I hope you will join me for this fun adventure because it will be so much fun.

Thanks for stopping by and if you have any questions or have spotted any errors, let me know. Take care and see you!

I know you want to say something, say it!