Objects

The object-oriented programming (OOP) paradigm has been around for many years now, although the degree to which it is support varies widely across languages. C++, for example, is object-oriented C, and, as some purists would say, implements more OOP functionality than even Java does.

Before PHP 5 came along, OOP support in PHP was quite flaky and more of a hack than a serious attempt. As a result the few that used it often regretted the choice, and it is not surprising that the whole system got a full rewrite in PHP 5 - it is now much more advanced and flexible, and should please just about everyone.

Topics covered in this chapter are:

  • Objects and classes defined

  • Class inheritance

  • Access control

  • Runtime type information

  • Abstract and final properties and functions

  • Constructors and destructors

  • Magic functions

Chapter contents

  1. 6.1. Conceptual overview
  2. 6.2. Classes
    1. 6.2.1. Defining a class
    2. 6.2.2. How to design your class
    3. 6.2.3. Basic inheritance
    4. 6.2.4. Overriding functions
  3. 6.3. Objects
  4. 6.4. Variables
  5. 6.5. The 'this' variable
  6. 6.6. Objects within objects
  7. 6.7. Access control modifiers
    1. 6.7.1. Public
    2. 6.7.2. Private
    3. 6.7.3. Protected
    4. 6.7.4. Final
    5. 6.7.5. Abstract
    6. 6.7.6. Iterating through object variables
  8. 6.8. Object type information
  9. 6.9. Class type hints
  10. 6.10. Constructors and destructors
    1. 6.10.1. Parent constructors
    2. 6.10.2. Destructors
    3. 6.10.3. Deleting objects
  11. 6.11. Copying objects
  12. 6.12. Comparing objects with == and ===
  13. 6.13. Saving objects
  14. 6.14. Magic functions
    1. 6.14.1. __autoload()
    2. 6.14.2. __get()
    3. 6.14.3. __set()
    4. 6.14.4. __call()
    5. 6.14.5. __toString()
  15. 6.15. Static data
  16. 6.16. Helpful utility functions
  17. 6.17. Interfaces
  18. 6.18. Deferencing object return values
  19. 6.19. The Object-Oriented Website
    1. 6.19.1. A basic OOP site
    2. 6.19.2. A more complex OOP website
  20. 6.20. Summary
  21. 6.21. Exercises
  22. 6.22. Further reading
  23. 6.23. Next chapter

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Conceptual overview >>

Previous chapter: Next chapter

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.