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