Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore in php function

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.

like image 226
Stepppo Avatar asked Nov 30 '09 16:11

Stepppo


People also ask

What starts with __ double underscore in PHP?

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.

What is double underscore in PHP?

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.


2 Answers

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

like image 93
Jacob Relkin Avatar answered Oct 12 '22 00:10

Jacob Relkin


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

like image 21
Emre Yazici Avatar answered Oct 12 '22 00:10

Emre Yazici