Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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 convention, you probably won't ever accidentally create a function with the same name as a magic method. Magic methods can be very useful. I can trigger custom behavior upon specific events, such as attempting to access a non-available property, or calling a method that doesn't exist.
I can also customize the creation of an object, and set defaults that aren't available at run time, such as the logged in user, or the current time. This all sounds good, but what's the cost? Magic methods have an overhead, leading to code execution that is anywhere from 3 to 20 times slower for that particular method call. They also ignore scope, meaning that you can accidentally expose a property or method that is normally hidden from the rest of the program. Finally, magic methods can break code completion in IDE's, meaning the IDE can follow the logic, and may assume that you're trying to access something that doesn't exist, and that you're wrong.
With all that said, weigh the pros and cons of any technique before implementing it. It may be worth your IDE unnecessarily warning you in exchange for a contextual response to a request for an inaccessible property. The performance difference is able to be detected in benchmarking with millions of iterations, but in practical use, the difference is pretty much nothing. Sometimes, it can be useful to have something that can logically decide whether or not to expose something hidden by a scope. My goal is to educate, and to allow you to determine what approach is best for your needs.
There is no one right answer. Well, that's enough time. Let's start using magic methods.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98389 Viewers
61 Video lessons · 85679 Viewers
71 Video lessons · 69607 Viewers
56 Video lessons · 101932 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.