1

When using OOP in PHP I've seen a lot of functions written like this: function __myfunc(){} I want to know what the underscores do. I've read they protect the function but from what and how?

Another example:

class myClass{
    function __myFunc(){
        return ' what am i doing?';
    }
}
$question = new myClass;
echo $question->__myFunc();
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
bashleigh
  • 8,813
  • 5
  • 29
  • 49
  • 1
    please look at this answer: http://stackoverflow.com/questions/1820973/underscore-in-php-function – eaykin Nov 25 '12 at 13:11

3 Answers3

3

The underscores have no effect. However, two underscores are used to indicate a magic function that has special meaning, e.g. the constructor __construct() or the destructor __destruct(). Some people used one or two underscores to indicate that a method is meant to be "private", i.e. used only internally. Since this feature is implemented in PHP >= 5 as a special keyword, you shouldn't use this "underscoring" anymore:

class myClass{
    private function myFunc() {
        return ' what am i doing?';
    }
}

$question = new myClass();
echo $question->myFunc(); // fails!
Niko
  • 26,516
  • 9
  • 93
  • 110
  • If you are using a framework like Zend Framework, you may wish to keep with the frameworks coding standard and use an underscore for the naming of private and protected methods to keep the code base consistent. – Carl Owens Nov 26 '12 at 14:20
2

This is basically a code notation that this method is private and the user of the code should not play with it. (critical to the overall functionality)

You should place an underscore symbol only on a private methods, it is considered a good practice.

Plamen Nikolov
  • 2,643
  • 1
  • 13
  • 24
  • Thanks for the answer I should be sure to do this next time I build a Private function. – bashleigh Nov 25 '12 at 13:15
  • The manual says "It is recommended that you do **not** use function names with __ in PHP unless you want some documented _magic_ functionality". So.. As I understand, you should **not** use it for *private* functions!? – eyecatchUp Nov 25 '12 at 13:22
  • We are discussing *one* underscore, but yes using *__* is not recommended as it will be misunderstood as a magic method. – Plamen Nikolov Nov 25 '12 at 13:24
  • Who said *one* underscore? The question example code has two. And the question was "what the underscore**s** do". Edit: Misread your answer, which indeed says **an** underscore. However, the question was about __ – eyecatchUp Nov 25 '12 at 13:26
  • Yea I did mean two. But now I know there's a difference between one and two underscores. Thanks again – bashleigh Nov 25 '12 at 13:44
1

There are some of the functions which are called magic others are just the notation to mark them as private - so if someone form your team see this function with underscore he will think of it as private. But actually you can use them outside of class itself. To protect methods inside class you need to use access modifiers

Dmitriy Sushko
  • 242
  • 1
  • 6
  • Thanks for the info. I've never coded in a team before so this should come in handy if I do. If I was to see this on any old function I should presume it works and I shouldn't tamper with it? – bashleigh Nov 25 '12 at 13:14
  • It depends of situation. I can write the code with all functions underscored but you can use all of them. But usually, yes, you don't need to use them directly. Usually they have "public" methods which are the interface - you need to use only public functions. – Dmitriy Sushko Nov 25 '12 at 13:32