11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 6 • OBJECT-ORIENTED <strong>PHP</strong>www.it-ebooks.infosuch details. The practice of separating the user from the true inner workings of an application throughwell-known interfaces is known as encapsulation.Object-oriented programming promotes the same notion of hiding the inner workings of theapplication by publishing well-defined interfaces from which each application component can beaccessed. Rather than get bogged down in the gory details, OOP-minded developers design eachapplication component so that it is independent from the others, which not only encourages reuse butalso enables the developer to assemble components like a puzzle rather than tightly lash, or couple,them together. These components are known as objects, and objects are created from a template knownas a class, which specifies what sorts of data the object might contain and the behavior one wouldexpect. This strategy offers several advantages:• The developer can change the application implementation without affecting theobject user because the user’s only interaction with the object is via its interface.• The potential for user error is reduced because of the control exercised over theuser’s interaction with the application.InheritanceThe many objects constituting our environment can be modeled using a fairly well-defined set of rules.For instance, all employees share a common set of characteristics: name, employee ID, and wage.However, there are many different types of employees: clerks, supervisors, cashiers, and chief executiveofficers, among others, each of which likely possesses some superset of those characteristics defined bythe generic employee definition. In object-oriented terms, these various employee types inherit thegeneral employee definition, including all of the characteristics and behaviors that contribute to thisdefinition. In turn, each of these specific employee types could be inherited by yet another more specifictype. For example, the Clerk type might be inherited by a day clerk and a night clerk, each of whichinherits all traits specified by both the employee definition and the clerk definition. Building on thisidea, you could then later create a Human class, and then make the Employee class a subclass of Human. Theeffect would be that the Employee class and all of its derived classes (Clerk, Cashier, Executive, etc.)would immediately inherit all characteristics and behaviors defined by Human.The object-oriented development methodology places great stock in the concept of inheritance.This strategy promotes code reusability because it assumes that one will be able to use well-designedclasses (i.e., classes that are sufficiently abstract to allow for reuse) within numerous applications.PolymorphismPolymorphism, a term originating from the Greek language that means “having multiple forms,” definesOOP’s ability to redefine, or morph, a class’s characteristic or behavior depending upon the context inwhich it is used.Returning to the example, suppose that a behavior titled clockIn was included within the employeedefinition. For employees of class Clerk, this behavior might involve actually using a time clock totimestamp a card. For other types of employees, Programmer for instance, clocking in might involvesigning on to the corporate <strong>net</strong>work. Although both classes derive this behavior from the Employee class,the actual implementation of each is dependent upon the context in which “clocking in” isimplemented. This is the power of polymorphism.These three key OOP concepts (encapsulation, inheritance, and polymorphism) are furtherintroduced as they apply to <strong>PHP</strong> through this chapter and the next.136

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!