PHP This! A Beginners Guide to Learning Object Oriented PHP. Michelle Gosney

Читать онлайн книгу.

PHP This! A Beginners Guide to Learning Object Oriented  PHP - Michelle Gosney


Скачать книгу
to summarize the three PHP access Modifiers in a more formal way:

      private restricts the access to the class itself. Only methods that are part of the same class can access private members. Let’s look at an example below:

      The reason why data members are declared private is to avoid the outside programs to unintentionally modify the values without necessary validation. If you make a data member private you should provide public getter/setter methods to access the value of this data member. The access modifier protected allows the class itself and all of it’s subclasses to access the data members and member functions. Global access is denied.

      public means that any code can access the member by its name.

Screen shot 2013-04-30 at 6.15.44 PM.png

      Now back to my previous point about object-oriented principles being “global” in the way a developer should think of them. I used Mindi's tattoo to explain access modifiers but I also had to demonstrate encapsulation and inheritance in order to do so.

      Inheritance and Encapsulation are really just ways of regulating the ability of an object defined that is defined by a class.

      Inheritance

      Recall Mindi’s tattoos, specifically the tattoo on her back? The tattoo on her back was described as protected in PHP terms meaning that it was visible within the Mindi class and to classes that extended the Mindi class. To inherit in PHP5, you should use the keyword extends in the class definition.

Screen shot 2013-04-30 at 6.16.40 PM.png

      Inheritance passes "knowledge" down. It is a structured way of extending code and functionality into new programs and packages. Classes are created in hierarchies and inheritance allows the structure and methods in one class to be passed down the hierarchy. That means less programming is required when adding functions to complex systems. If a step is added at the bottom of a hierarchy, then only the processing and data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object oriented programming.

      Method Overriding

      Method overriding is when the function of base class is re-defined with the same name, function signature and access modifier (either public or protected) of the derived class.

      The reason to override a method is to provide additional functionality over and above what has been defined in the base class. Imagine that you have a class by the name of Beverage from which you derive two child classes, Martini and Scotch. The Beverage class has methods defined to stir, mix, etc, but each of the specialized classes Martini and Scotch will have its own style of mixing and stirring and hence would need to override the stir and mix functionality.

      Lets look at an example with Beverage:

Screen shot 2013-04-30 at 7.47.27 PM.png

      Invoking Parent Methods

      First of all, “invoke” means to call, to request the action of something. I just wanted to get that brief explanation out of the way. Now, when you override a method of the base class, its functionality is completely hidden unless it has been explicitly invoked from the child class. To invoke a parent class method you should use the keyword parent followed by the scope resolution operator “:: “ followed by the name of the method as mentioned below:

Screen shot 2013-04-30 at 6.20.17 PM.png

      Look at the example below:

Screen shot 2013-04-30 at 6.21.04 PM.png

      In the above example, look at the way in which the showData() function in the Employee child class is invoking the Person parent class’s showData() function. When the program executes the showData() method if the Employee class is called which in turn calls the showData() function of the parent class. After the parent class’s showData() function completes its execution the remaining code in showData() function of the Employee class is executed.

      Horizontal Inheritance – Using Traits

      PHP does not support multiple inheritance; a class can only extend a single base class. What happens if you need code from more than one class? Until recently, developers had to accomplish this in a crude manner by copying and pasting code from additional classes that was needed outside of its base class. Now this can be done by way of a new feature introduced in PHP5.4 called a trait that provides a means of horizontal inheritance.

      A trait looks like a class in the way it is structured but it cannot be instantiated. Properties and methods can be defined within a trait structure but they can only be used by a class that incorporates them with the use keyword which is called within the class that uses it, that is, it goes inside the curly braces. It isn’t called in the class definition outside the curly braces like interfaces and the extending class (we will get to interfaces in Chapter 5).

Screen shot 2013-04-30 at 6.22.47 PM.png

      Encapsulation

      Encapsulation means what the term implies, to put in a “capsule,” to place functionality into a single package. Consider an object as a capsule or a box that has things in it. The access modifiers are like windows that control who gets to see inside with ‘public’ access being a wide open window that let's everyone see inside and ‘private’ access being fully shaded that doesn't let anyone see inside.

      The control over the visibility of the contents of an object is a big part of how encapsulation works.

      The contents of an object are properties and methods and because they are all packaged inside an object they make up a sort of mini-program that has control over how visible its contents, data, are. This is encapsulation.

      Technically speaking, encapsulation refers to the creation of self-contained modules that bind processing functions to the data. Encapsulation ensures good code modularity, which keeps routines separate and less prone to conflict with each other. To reiterate, you can think of each object as a mini program that handles it’s own responsibilities as it’s own self contained capsule or pill.

      Polymorphism

      Now that you know that a class is a blueprint and that an object is constructed based on that class it is important to understand that many different objects can be created from the same class. I may have a single class for a table but I can create many different kinds of table objects from that class. I could have a round table, a square table, a long table a short table. I could have a blue table or a brown table. I could have a table made of oak or one made from plastic; all from the same base class.

      Polymorphism simply means many forms. In computer programming, polymorphism is the ability to create a variable, a function, or an object that has more than one form in order to provide capability of adapting to what needs to be done. It is a mechanism by which objects of different types can process data through a single interface. Polymorphism is used to make applications more modular and extensible. Instead of messy, hard to read conditional statements describing different courses of action, you create interchangeable objects that you select


Скачать книгу