At university I studied primarily in C# .NET and have been working as a professional .NET web developer since 2010. Described as an object-orientated and type-safe language, there is an awful lot of discipline to how the framework has been set out. From a developers perspective it becomes second nature to identify patterns of the code language you are working with and this helps learning. This is certainly the case with .NET, with the small exception of Tuples, but we all make mistakes.

Deciding to become a freelancer in 2016 led to me working with WordPress and PHP. It’s just too popular to ignore and also gives clients with smaller budgets an affordable alternative option (compared to the .NET path). I’ve come to understand the quirks of PHP during the years, but I still can’t understand how such inconsistencies came to be allowed into the framework in the first place.

I can only assume it was operated by a band of developers who didn’t communicate well with each other. Or, didn’t have a clear set of coding standards to adhere to. Then again maybe I just see it differently due to my experience with .NET

The “No Naming Convention” of PHP

grayscale photography of crying woman

str_replace vs strpos is a good case for this argument. Clearly these are both string type utilities, which we can see from the str prefixes. So, as they belong to the same family I would expect some similarities in their usage and naming.

However, str_replace seemes to be following a snake case naming style whereas strpos is following a flat case naming style. Why are they different? That’s weird, but let’s make an exception as everyone should be allowed at least one free pass.

Now let’s consider the parameters, both str_replace and strpos expect 5 parameters in total all with different names. But they actually do similar things in the following cases:

  • $search is equal to $needle
  • $subject is equal to $haystack

In fact $needle and $haystack are common names to appear in other PHP methods, so who the hell came up with $search and $subject? Further to this the ordering could match in the case of the first two parameters. This is not critical, but the difference in naming and order does cause pause for thought whilst you mentally check yourself.

Have I placed that parameter correctly? Do I need to google this to make sure?

It all comes down to the same subtleties that makes mastering a language more achievable. If there is no standard of coding then you have to take each new learning with a “from scratch” perspective.

How it should look

teacher giving out instructions not to cheat

I’m not really a fan of snake case or flat case, I prefer PascalCase for method names as I feel it gives a bit more authority over parameter names. But, as we’re dealing with PHP I’ll go with snake case as the lesser of two evils.

With all said and done, this is how I would write the two methods

str_replace( $haystack, $needle, $replace)

str_pos( $haystack, $needle )

“str_” indicates the family of utilities we are dealing with (almost like a namespace or class name) followed by the operation. Then we maintain a pattern of subject followed by query.

This method is now obsolete

Unfortunately, there is no safe way to move to a more robust coding standard without risking depreciation issues. And to be fair, PHP developers don’t deserve the headache of going back to their old systems to correct a styling problem that shouldn’t have been allowed in the first place.

In short we’re stuck with it. So, thank you to the PHP 4 team who introduced this nonsense – you guys are legends and your legacy is a steaming pile of, dare I say it, poop.

Leave a Reply

Your email address will not be published. Required fields are marked *