Home » Php » PHP: Why are some people naming functions with '_' in the beginning?

PHP: Why are some people naming functions with '_' in the beginning?

Posted by: admin July 12, 2020 Leave a comment

Questions:

Why do some people call their functions e.g. ‘__makethis()’ and ‘_makethat()’?

Is this any special feature or is this just in fashion?

thx for your answers 😉

How to&Answers:

The double underscore can sometimes be magic functions used by the PHP classes.

The single underscore can be part of their own naming convention for functions. But usually it means that a function is private, back in PHP4 classes didn’t support private (or protected) functions, so people fake a private function that way (tho the function is not private in reality).

Answer:

That’s a convention for naming private and protected methods.

Answer:

Usually private members of a class are prefixed with a ‘_’ sign. Check out the PEAR code convention.

Magic methods of a class are prefixed with __ . I suggest if you write a method that performs some magic, prefix it with __ .