Viewed   65 times

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.

 Answers

3

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.

Wednesday, October 19, 2022
1

There's really not a single, common standard for these things. Most languages are more restrictive than PHP in this sense.

In the later years, a lot of so-called frameworks have emerged, and amongst other things they define a set of rules for everything from naming over where to place files and to which style your code should follow. There are several frameworks around, so you can't really pick one and call it the standard. However, most frameworks have a subset of commonality. For example, most follows some variant of the PEAR Coding Standards.

Sunday, December 4, 2022
 
3

After assigning the function to the property, the object has a method called changeName and a property called changeName. Which then does ->changeName() refer to? Is it ($testCls->changeName)() or ($testCls->changeName())? The answer is that the existing method wins out. You cannot overwrite or replace a method this way.

You can call the property function like this:

call_user_func($testCls->changeName, 'Some name');

However, this will throw this error:

Fatal error: Using $this when not in object context

Because $this inside the anonymous function you assigned does not refer to $testCls, it refers to nothing, since there is no $this in the scope where the function was defined.

In other words, this won't work at all the way you want it to.

Sunday, August 14, 2022
2

It's perfectly valid to leave out the break when you return from a switch.

But it's fairly common practise to add explicit breaks to every case as a defensive programming practise.

switch ($foo) {
    case 1:
        return 1;
        break;

    case 2:
        return 2;
        break;
}

The idea is that should you later change your code in case 1 and remove the return statement, you could forget to add a break.

That would accidentally cause program flow to fall through to case 2.

switch ($foo) {
    case 1:
        somethingDifferent();

    case 2:
        return 2;
        break;
}

Falling through case statements is slightly unusual and you should add a comment to your code when you do it to show that it's intentional.

switch ($foo) {
    case 1:
        somethingDifferentAndWeWantToDoCase2AsWell();
        // fallthrough

    case 2:
        return 2;
        break;
}

As with many defensive programming practises you've got to balance whether the code bloat - which potentially clutters your code and make it less readable - is worth it or not.

Thursday, August 4, 2022
1

I'm just giving you the super lazy option:

function __call($name, $args) {
    $name = $name . "_" . implode("_", array_map("gettype", $args)));
    return call_user_func_array(array($this, $name), $args);
}

That would for example invoke the real function name getPrice_string_array for two parameters of that type. That's sort of what languages with real method signature overloading support would do behind the scenes.

Even lazier would be just counting the arguments:

function __callStatic($name, $args) {
    $name = $name . "_" . count($args);
    return call_user_func_array(array($this, $name), $args);
}

That would invoke getPrice_1 for 1 argument, or getPrice_2 for, you guessed it, two arguments. This might already suffice for most use cases. Of course you can combine both alternatives, or make it more clever by search for all alternative real method names.

If you want to keep your API pretty and user-friendly implementing such elaborate workarounds is acceptable. Very much so.

Saturday, September 3, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :