Page 186 - Open Soource Technologies 304.indd
P. 186

Web Technologies-I



                   Notes                 echo “I’m dead now :(“;
                                     }
                                 }
                                 $animal = new Animal(“Bob”);
                                 echo “Name of the animal: “ . $animal->name;

                                 ?>
                                 As you can see, the destructor is just like a constructor, only the name differs. If you try running
                                 this example, you will see first the constructor message, then the name of the animal that we
                                 manually output in the last line, and after that,  the script ends, the object is destroyed, the
                                 destructor is called and the message about our poor animal being dead is outputted.


                                                The OOPs functionality was introduced in PHP version 3.


                                 8.5.2 Visibility
                                 Visibility is a big part of OOP. It allows you to control where your class members can be accessed
                                 from, for instance to prevent a certain variable to be modified from outside the class. The default
                                 visibility is public, which means that the class members can be accessed from anywhere. This
                                 means that declaring the visibility is optional, since it will just fall back to public if there is no
                                 access modifier. For backwards compatibility, the old way of declaring a class variable, where
                                 you would prefix the variable name with the “var” keyword (this is from PHP 4 and should
                                 not be used anymore) will also default to public visibility.
                                 PHP is pretty simple in this area, because it comes with only 3 different access modifiers: private,
                                 protected and public.

                                 Private members can only be accessed from inside the class itself.
                                 Protected members can only be accessed from inside the class it self and its child classes.

                                 Public members can be accessed from anywhere outside the class, inside the class it self and
                                 from child classes.
                                 8.5.3 Inheritance

                                 Inheritance is one of the most important aspects of OOP. It allows a class to inherit members
                                 from another class. Understanding why this is smart without an example can be pretty difficult,
                                 so let’s start with one of those.

                                 Imagine that you need to represent various types of animals. You could create a Cat class, a
                                 Dog class and so on, but you would probably soon realize that these classes would share quite
                                 a bit of functionality. On the other hand, there could be stuff that would have to be specific for
                                 each animal. For a case like this, inheritance is really great. The idea is to create a base class, in
                                 this case called Animal, and then create a child class for each specific animal you need. Another
                                 advantage to this approach is that you will every animal you have will come with the same
                                 basic functionality that you can always rely on.

                                 Again, this can seem very theoretic and you might not find it very useful in the beginning, but
                                 as you create more advanced websites, you will likely run into situations where inheritance can
                                 come in handy. Let’s have a look at an example now:




        180                               LOVELY PROFESSIONAL UNIVERSITY
   181   182   183   184   185   186   187   188   189   190   191