What is a magic method, and do I need one?
Video: 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.
Viewers: in countries Watching now:
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.
- 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
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 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.
Find answers to the most frequently asked questions about Object-Oriented Programming with PHP .
Here are the FAQs that matched your search "" :
- Q: I'm not seeing warnings or errors in my environment like the video; why not?
- A: Your PHP configuration is probably configured not to show them to you. This is often true on commercial web hosts and is often the default. Fortunately, there are multiple ways of resolving this.
The easiest way would be to explicitly enable error reporting at the top of the PHP script you wish to debug.
error_reporting(E_ALL | E_STRICT);
Alternatively, if you have access to your php.ini file and you want to always have error reporting on, change error_reporting = to a development friendly value of
error_reporting = E_ALL | E_STRICT
then change display_errors = to
display_errors = On
Finally, if access to the php.ini file is not available, you can add the following directives to your .htaccess or VirtualHost configuration for Apache:
php_value error_reporting 32767
php_value display_errors 1
If you would like a development optimized development environment like the one utilized in this course, see Up and Running with Linux for PHP Developers, here in the lynda.com online training library.
Q: This course was updated on 4/10/2013. What changed?
A: The author rerecorded some of the tutorials to add more background information and better graphics.
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.