What are magic methods and how are they used in php?
Answer Posted / Tanya
Magic methods, also known as special methods or magic functions, are predefined function names that allow you to perform various actions within PHP objects. Magic methods start with double underscores (`__`) before the method name and can be overridden in your custom classes to customize their behavior.
Here are some commonly used magic methods:
1. `__construct()`: This is called when an object is created. It allows you to initialize properties of the class.
2. `__destruct()`: This is called when an object goes out of scope or is destroyed. It's useful for cleaning up resources like database connections, file handles, and other system resources.
3. `__get()`: This method is used for accessing non-existent properties in an object.
4. `__set()`: This method is used for setting non-existent properties in an object.
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers