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();
1

3 Answers 3

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!
Sign up to request clarification or add additional context in comments.

1 Comment

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

5 Comments

Thanks for the answer I should be sure to do this next time I build a Private function.
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!?
We are discussing one underscore, but yes using __ is not recommended as it will be misunderstood as a magic method.
Who said one underscore? The question example code has two. And the question was "what the underscores do". Edit: Misread your answer, which indeed says an underscore. However, the question was about __
Yea I did mean two. But now I know there's a difference between one and two underscores. Thanks again
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

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.