What Is a Magic Method, and Do I Need One?


show more What is a magic method, and do I need one? provides you with in-depth training on Developer. Taught by Jon Peck as part of the Object-Oriented Programming with PHP show less
please wait ...

What is a magic method, and do I need one?

So, far we've barely scratched the surface of PHP's object-oriented support. In this chapter, I am going to show you how to take advantage of some of PHP's built-in methods that have a unique name: magic methods. I'll start by describing just what magic methods are and then we'll see how to write code that is able to make logical guesses when something is missing. We'll explore how to customize how objects are created, and then finally, make an object display itself. So, what exactly is a magic method? Magic Methods are a collection of specialized methods that have been built into PHP that execute in response to a particular event.

There are about a dozen magic methods available. The naming convention for magic methods is consistent. Each magic method name starts with two underscores, such as _construct() and _toString(). I can't name a method with one of these special names without triggering the magical behavior. With that said, due to the unique naming conve...

What is a magic method, and do I need one?
Video duration: 2m 23s 1h 57m Intermediate Updated Apr 10, 2013

Viewers:

What is a magic method, and do I need one? provides you with in-depth training on Developer. Taught by Jon Peck as part of the Object-Oriented Programming with PHP

Subject:
Developer
Software:
PHP
Author:
please wait ...