Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, December 22, 2015

What's the deal with a leading underscore in PHP class methods?

What's the deal with a leading underscore in PHP class methods?


While looking over various PHP libraries I've noticed that a lot of people choose to prefix some class methods with a single underscore, such as

public function _foo()  

...instead of...

public function foo()  

I realize that ultimately this comes down to personal preference, but I was wondering if anyone had some insight into where this habit comes from.

My thought is that it's probably being carried over from PHP 4, before class methods could be marked as protected or private, as a way of implying "do not call this method from outside the class". However, it also occurred to me that maybe it originates somewhere (a language) I'm not familiar with or that there may be good reasoning behind it that I would benefit from knowing.

Any thoughts, insights and/or opinions would be appreciated.

Answer by jonstjohn for What's the deal with a leading underscore in PHP class methods?


Leading underscores are generally used for private properties and methods. Not a technique that I usually employ, but does remain popular among some programmers.

Answer by Quintin Robinson for What's the deal with a leading underscore in PHP class methods?


I believe your original assumption was correct, I have found it to be common practice for some languages to prefix an underscore to methods/members etc that are meant to be kept private to the "object". Just a visual way to say although you can, you shouldn't be calling this!

Answer by Jeremy DeGroot for What's the deal with a leading underscore in PHP class methods?


It's from the bad old days of Object Oriented PHP (PHP 4). That implementation of OO was pretty bad, and didn't include things like private methods. To compensate, PHP developers prefaced methods that were intended to be private with an underscore. In some older classes you'll see /**private*/ __foo() { to give it some extra weight.

I've never heard of developers prefacing all their methods with underscores, so I can't begin to explain what causes that.

Answer by GloryFish for What's the deal with a leading underscore in PHP class methods?


I use a leading underscore in the PHP 5 class I write for private methods. It's a small visual cue to the developer that a particular class member is private. This type of hinting isn't as useful when using an IDE that distinguishes public and private members for you. I picked it up from my C# days. Old habits...

Answer by joedevon for What's the deal with a leading underscore in PHP class methods?


I believe the most authoritative source for these kinds of conventions for PHP right now would be the PSR-2: Coding Style Guide because the Zend Framework is part of PSR:

Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.

Answer by Matthew for What's the deal with a leading underscore in PHP class methods?


I know it from python, where prefixing your variables with an underscore causes the compiler to translate some random sequence of letters and numbers in front of the actual variable name. This means that any attempt to access the variable from outside the class would result in a "variable undefined" error.

I don't know if this is still the convention to use in python, though

Answer by Tristan for What's the deal with a leading underscore in PHP class methods?


They are called "magic methods".

Answer by umpirsky for What's the deal with a leading underscore in PHP class methods?


I was strongly against prefixing private/protected methods with underscore since you can use private/protected keyword for that and IDE will mark it for you.

And I still am, but, I found one reason why it can be a good practice. Imagine that you have public method addFoo() and inside that method you have some part of task which is common with other methods addFooWhenBar(), addFooWhenBaz()... Now, best name for that common method would be addFoo(), but it is already taken, so you must come up with some ugly name like addFooInternal() or addFooCommon() or ... but _addFoo() private method looks like best one.

Answer by Sliq for What's the deal with a leading underscore in PHP class methods?


Now, in 2013, this is "officially" bad style by the PSR-2 coding guideline:

Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility`

Source: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

Answer by maho for What's the deal with a leading underscore in PHP class methods?


In Drupal (a php CMS) underscores can be used to prevent hooks from being called (https://api.drupal.org/api/drupal/includes!module.inc/group/hooks/7).

If I have a module called "my_module" and want to name a function my_module_insert it would "hook" on the function hook_insert. To prevent that I can rename my function to _my_module_insert.

ps The way hooks works in Drupal it's possible to implement a hook by mistake, which is very bad.

Answer by user3613677 for What's the deal with a leading underscore in PHP class methods?


Drupal, and using underscore:

In a general way the underscore is to simple mark the fact that a function would probably only be called by a related parent function...

function mymodule_tool($sting="page title"){    $out ='';    //do stuff     $out  .= _mymodule_tool_decor($sting);  return $out;  }    function _mymodule_tool_decor($sting){    return '

'.$string.'

'; }

of course, just a simple example...


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.