Instead of just asking an object to display itself, it's more convenient to assume…that when echoed, an object knows to treat itself like a string.…At a high level, this concept is known as polymorphism, where I can act on an…object without knowing exactly what the class is. In practice, this means more…common function names between classes.…I am going to cover polymorphism in greater depth in a later segment.…For now, let's focus on making an address display as a string by echoing.…To do this, I'm going to use the magic method _toString(), which allows an object…to specify how to return a string.…

I am going to switch back to the demo script. Then, at the end, add the following…lines: echo <h2>Address _toString. Then echo $address_2. Refresh your browser.…You will see a catchable fatal error.…This is because the object does not have a method defined to convert itself to a string.…Open the Address class. Under the Magic _set() method, declare a new method…called _toString. Add the PHP documentation. Magic _toString returns a string…

Resume Transcript Auto-Scroll

Author

Updated

4/10/2013

Released

9/26/2012

Whether you're enhancing or optimizing existing code or just starting from scratch, there's never a better time to start integrating object-oriented design techniques. This course shows how to integrate the principles of object-oriented programming into the build of a PHP-driven web page or application. After an overview of what objects and classes are and why they should be used, author Jon Peck dives into creating and instantiating objects, then defining the class relationships and interactions that will form the basis of your coding arsenal. The course also shows how to leverage PHP objects and implement design patterns, and looks at steps you can take to continue adding to your programming tool belt.

Topics include:

Historical overview of object-oriented PHP

Defining classes

Creating a method/object context with $this

Accessing classes without instantiation

Creating a database class

Extending and abstracting classes

Cloning and comparing objects

Error handling with exceptions

Implementing design patterns, such as the factory and strategy patterns