The hidden genius of Trongate's underscore naming convention
David Connelly David Connelly
11.4K subscribers
360 views
17

 Published On May 27, 2024

One of the more controversial aspects of Trongate has been its use of the underscore naming convention. This practice involves prefixing method names with an underscore to prevent these methods from being invoked via URLs. For example:

function _secret() {
echo "Secret information ahoy!";
}

Trongate is not the first PHP framework to adopt this convention. Previously, frameworks like Zend Framework 1, Yii 1, and earlier versions of CodeIgniter all used this convention. However, with the evolution of PHP and its support for visibility keywords, this practice has become less common in favor of explicitly defining method visibility (public, protected, private).

So, what's going on here? Is Trongate just an old-fashioned framework?

Even if Trongate's usage of the underscore method can be justified in terms of giving developers fewer characters to type, one would have to ask, "Is that enough justification for holding back the hands of time and doing something that none of the other modern PHP frameworks are currently doing?" I don't think it is. I find shortcuts (or shorthand ways of doing things) to be unsatisfying and intellectually bankrupt. Don't get me wrong, shortcuts are nice. Holy C - the programming language that was invented by Terry Davis - is full of shortcuts. By that I mean that he found ways of doing things that require fewer characters than with C / C++. It's nice. It's interesting. However, "it uses fewer characters" is not a compelling enough reason for asking developers to change their way of doing things. We need more. We need to do better!

I'm sure that in a week or so, it would be possible to go through the entire Trongate framework and ditch all of those pesky "underscore" methods. Instead, we could have all of the methods being named properly and being all assigned as "public," "private," or "protected." If such an upgrade were to happen, we may suffer some breaking changes for a few days, but it would be worth it because - in the end - Trongate would be falling into line with the rest of the PHP community and we'd finally be doing "proper PHP." Seems like a tempting idea, doesn't it?

Alas! None of the preceding paragraph is true.

The main problem here is that the case for ditching the underscore naming convention is based on flawed logic and a fundamental misunderstanding of the nature of OOP. Put simply, using the underscore naming convention is not synonymous with using access modifiers like "private" or "protected."

In Trongate, when you have two controller classes and each class is contained within its own module (let's call it "Module A" and "Module B"), those two modules are entirely distinct and independent of each other. You can think of them as being two entirely independent PHP classes. "Module A" does not know about "Module B." It does not extend Module B. It does not depend upon Module B. Even though both modules extend the main Trongate class, "Module A" works entirely independently of "Module B."

This means that the only way for "Module A" to load a method from "Module B" would be if the target method (on "Module B", in this instance) happens to be "public." You can set up a little test and try this for yourself. I guarantee, you cannot invoke "Module B" from "Module A" unless you're invoking a "public" method. The instant you change a method to "protected" or "private," it immediately becomes unusable for other modules. Please don't take my word for it. If you have the time, I encourage you to set up a little test and see for yourself. Everything I have just described is perfectly in alignment with the rules of OOP.

Contrary to what you might think, Trongate's usage of the underscore method is not a shorthand way of making a method "private" or "protected." Instead, it's a means of modifying methods so that they cannot be invoked via the URL.

The above distinction is nuanced and subtle. What it means (and it took me several days to get my head around this) is that - for Trongate - the underscore naming convention is a necessity. That seemingly antiquated way of doing things is a critical component of Trongate's truly modular architecture. If we did not use the underscore naming convention, then we'd need to find another way to tell Trongate that "you should not be able to invoke this method via 'this' URL."

Ultimately, framework makers all have to solve a key technical challenge, which is, "How do we stop methods from being invoked via the URL?"

Trongate's solution is crystal clear, and it only requires one character to be typed - an underscore. If Trongate did not use the underscore naming convention, then we'd have to start looking at very slow and miserable alternatives such as YAML files - as we see with Symfony.

Remember, those of us who use Trongate have the fastest PHP framework in the world (prove me wrong!). It is for other PHP frameworks to come to us - not the other way around.

DC

show more

Share/Embed