Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User defined magic methods: What is "documented magic functionality"?

My question is rather simple, here is the context:

http://php.net/manual/en/language.oop5.magic.php

Magic Methods

The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state() and __clone() are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.

PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

I get what these methods are for and how to use them. What I don't understand is this:

...unless you want some documented magic functionality.

What does that even mean? Are there actual reasons to create user defined __magicMethods()?

like image 205
Wesley Murch Avatar asked Mar 14 '12 11:03

Wesley Murch


People also ask

Which magic method is called when a user calls an object as a function?

__invoke(): This method is defined in a class that will be called while trying to call an object in a way of calling function.

What are magic methods?

Magic methods are special methods in python that have double underscores (dunder) on both sides of the method name. Magic methods are predominantly used for operator overloading.

What is magic method explain with example?

Magic methods are special methods which override PHP's default's action when certain actions are performed on an object. Caution. All methods names starting with __ are reserved by PHP. Therefore, it is not recommended to use such method names unless overriding PHP's behavior.

How many magic methods are there in PHP?

Another aspect of PHP overloading capabilities is property overloading. There are four magic methods that support property overloading: __get(), __set(), __isset() and __unset().


2 Answers

I think that they only mean that it's better not to use __ as a starting name for your methods because PHP has reserved that convention for his magic methods and if you do use that for a method it might get overriden in the future and have some magic functionality. At least that's how i understood that

EDIT - to be even clearer: Let's say thatyou implement for your own business logic a method called __toNumber(). In a future version of PHP they decide that whenever an object is used as a number (maybe when you do $result = 3 * $yourObject) the magic method __toNumber() will be invoked...your object will have some "Magic" documented functionality even if you didn't specifically add it

like image 110
Nicola Peluchetti Avatar answered Sep 22 '22 13:09

Nicola Peluchetti


It means never use names that starts with __ for functions unless you want the magic functionality documented in the PHP manual.

like image 23
meze Avatar answered Sep 20 '22 13:09

meze