11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

www.it-ebooks.infoCHAPTER 7 • ADVANCED OOP FEATURESExecuting this code returns the following output:Drone1 employeeID: 12345Drone2 employeeID: 67890Drone2 tie color: blueInheritancePeople are adept at thinking in terms of organizational hierarchies; we make widespread use of thisconceptual view to manage many aspects of our everyday lives. Corporate management structures, theDewey Decimal system, and our view of the plant and animal kingdoms are just a few examples ofsystems that rely heavily on hierarchical concepts. Because OOP is based on the premise of allowinghumans to closely model the properties and behaviors of the real-world environment we’re trying toimplement in code, it makes sense to also be able to represent these hierarchical relationships.For example, suppose that your application calls for a class titled Employee, which is intended torepresent the characteristics and behaviors that one might expect from a company employee. Someclass properties that represent characteristics might include the following:• name: The employee’s name• age: The employee’s age• salary: The employee’s salary• yearsEmployed: The number of years the employee has been with the companySome Employee class methods might include the following:• doWork: Perform some work-related task• eatLunch: Take a lunch break• takeVacation: Make the most of those valuable two weeksThese characteristics and behaviors would be relevant to all types of employees, regardless of theemployee’s purpose or stature within the organization. Obviously, though, there are also differencesamong employees; for example, the executive might hold stock options and be able to pillage thecompany while other employees are not afforded such luxuries. An assistant must be able to take amemo, and an office manager needs to take supply inventories. Despite these differences, it would bequite inefficient if you had to create and maintain redundant class structures for those attributes that allclasses share. The OOP development paradigm takes this into account, allowing you to inherit from andbuild upon existing classes.Class InheritanceClass inheritance in <strong>PHP</strong> is accomplished by using the extends keyword. Listing 7-3 demonstrates thisability, first creating an Employee class and then creating an Executive class that inherits from Employee.163

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

Saved successfully!

Ooh no, something went wrong!