What does it mean when a PHP function name begins with an underscore?
for example: __construct()
I know what the construct means but I've seen other places where the function begins with an underscore, or a double underscore and I'm just not sure of the significance.
Whenever you see a function name start with a double-underscore, it is a "magic" function - one that PHP has provided that you have not declared yourself.
Strictly speaking, it means nothing in PHP as it is not a pre-defined function. However, in many frameworks, like CakePHP, and other libraries the double underscore is a function used for translating strings based on the user's language/locale preference.
It means that PHP calls it implicitly.
It's called a 'Magic Method'
Also, it's two underscores, not one.
Learn more here: PHP Magic Methods
In PHP, functions start with two underscores usually have special meanings. From the manual:
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.
For example __construct() is a special method which is called automatically while initializing an object.
Se also: http://php.net/manual/en/language.oop5.magic.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With