What's the deal with a leading underscore in P

2019-01-03 08:39发布

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.

14条回答
你好瞎i
2楼-- · 2019-01-03 09:09

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 '<h1>'.$string.'</h1>';
}

Of course, just a simple example...

查看更多
Luminary・发光体
3楼-- · 2019-01-03 09:09

I was looking for the same answer, I did some research, and I've just discovered that php frameworks suggest different styles:

Code Igniter

The official manual has a coding style section that encourages this practice:

Private Methods and Variables

Methods and variables that are only accessed internally, such as utility and helper functions that your public methods use for code abstraction, should be prefixed with an underscore.

public function convert_text()

private function _convert_text()

Other frameworks do the same, like

Cakephp:

does the same:

Member Visibility

Use PHP5’s private and protected keywords for methods and variables. Additionally, non-public method or variable names start with a single underscore (_). Example:

class A
{
    protected $_iAmAProtectedVariable;

    protected function _iAmAProtectedMethod()
    {
       /* ... */
    }

    private $_iAmAPrivateVariable;

    private function _iAmAPrivateMethod()
    {
        /* ... */
    }
}

And also

PEAR

does the same:

Private class members are preceded by a single underscore. For example:

$_status    _sort()     _initTree()

While

Drupal

code style specifically warns against this:

  1. Protected or private properties and methods should not use an underscore prefix.

Symphony

on the other hand, declares:

Symfony follows the standards defined in the PSR-0, PSR-1, PSR-2 and PSR-4 documents.

查看更多
趁早两清
4楼-- · 2019-01-03 09:11

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.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-01-03 09:14

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.

查看更多
爷、活的狠高调
6楼-- · 2019-01-03 09:19

They are called "magic methods".

查看更多
萌系小妹纸
7楼-- · 2019-01-03 09:20

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...

查看更多
登录 后发表回答