2

I am writing a huge PHP class and starting to realise that I lack some knowledge of a few PHP coding conventions that could help me organise a bit.

My question is about using ONE underscore before a PHP function's name (not two, I know what two underscores do) :

  • Is it actually a PHP convention for private functions ?
  • Is it in any way interpreted as being something specific by certain frameworks, template engines, etc ? (like /** is caught by PHPdocumentor)
  • Is there any merit apart from immediately being able to tell if a function is public or private ?
LocoNeko73
  • 35
  • 1
  • 6
  • 1
    There are **many** varying conventions used in PHP applications. You're on the right track though... a lot of folks use underscores to indicate that a function is private. – Brad Dec 26 '13 at 05:19
  • use php codesniffer for checking your php file – Viswanath Polaki Dec 26 '13 at 05:21
  • 1
    You can find the answer below :) [this is to clarify the meaning of underscores in php function in old OOP in PHP4 ][1] [1]: http://stackoverflow.com/questions/663350/whats-the-deal-with-a-leading-underscore-in-php-class-methods – uzaky Dec 26 '13 at 05:22
  • Thanks for your answer ! Not being sure of how to write "good" code is the most annoying part of being an amateur : I'm always double-guessing myself and end up feeling ashamed at showing my code... ;-) – LocoNeko73 Dec 26 '13 at 05:23
  • It's true that the question might have been answered by the "what's the deal with a leading underscore in PHP class methods", but only partially, as I wanted to know about points 2) & 3) above, too. Anyway, thanks all for your answers ! – LocoNeko73 Dec 26 '13 at 06:52

1 Answers1

4

The use of leading underscores is somewhat dictated by opinion, though it could be seen as a form of Hungarian notation, a "crutch" that programmers use to know what type of function it is.

Disregarding magic methods, double underscore would have been used to indicate private methods whereas a single underscore would mean a protected method.

This convention was introduced because PHP 4 OOP didn't have visibility modifiers such as protected and private. With the introduction of PHP 5, you no longer need this.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309