Yesterday, one of the biggest upgrades in the history of the Trongate framework was rolled out, and I'm delighted to say nobody appears to have noticed. This most recent upgrade has been in the pipeline for the best part of three years. Finally... it's done. I'm pleased to declare that Trongate now has doc blocks, type hinting, return types, and access modifiers (such as 'public', 'private', and 'protected') throughout the entire framework. The last time I looked into this (about six months ago), I could only find one other PHP framework that had all of these elements. Now, it's something that Trongate has. For normal Trongate developers, this massive upgrade signifies ...wait for it ...nothing.

So, What's The Point?

You may well wonder, "What's the point in investing massive amounts of time into an upgrade that people won't notice?"

Well, let's pump the brakes for a moment. Even though this counts as being a huge upgrade for the framework, it was made far easier thanks to a little help from the "employee of the month." By that, I mean Chat GPT. Anyone who's a member of my channel on YouTube will have surely noticed that Chat GPT has been doing a lot of the heavy lifting for Trongate, as of late. There's no doubt that AI has fundamentally changed web development forever. So, this upgrade was mostly the work of Chat GPT. There's no denying it.

Mind you, even with Chat GPT doing all of the heavy lifting, this particular upgrade amounted to a solid amount of work. There are really three reasons why this upgrade was important:

  1. It was necessary for the new documentation.
  2. It allows certain IDEs to take advantage of auto-complete features.
  3. It brings a degree of consistency to the framework.

Understanding The Upgrade

As I'm sure you've noticed, PHP frameworks are predominantly made of functions and methods. A function is a block of code that performs a specific task and can be reused throughout the code. A method - on the other hand - is simply a function that exists inside a class. The overwhelming majority of code that makes up the Trongate framework is functions and methods. Below is an example of a simple PHP method named 'can_vote':


function can_vote($name, $age) {
    if ($age < 18) {
        return false;
    }
    
    return "Hello $name, you are eligible to vote!";
}

The method above accepts two arguments, $name and $age. The first argument is expected to be a string. The second argument is expected to be an integer. Then, depending on what values have been passed into the method, it will either return a string or a boolean. I'm sure some developers might have disagreements about things like the positioning of curly brackets or perhaps they may take issue with my usage of snake case. Nevertheless, the method above is perfectly valid PHP.

When we add doc blocks, type hinting, return types, and access modifiers to the method, we end up with something that looks more like this:


/**
 * Check if a user is eligible to vote.
 *
 * This method checks if the given age is 18 or older. If so, it returns
 * a greeting message. Otherwise, it returns false.
 *
 * @param string $name The name of the user.
 * @param int $age The age of the user.
 * @return string|bool A greeting message if the user is eligible to vote, false otherwise.
 */
public function can_vote(string $name, int $age): string|bool {
    if ($age < 18) {
        return false;
    }

    return "Hello $name, you are eligible to vote!";
}

As you can see, that's a massive amount of characters that have been added! What we now have is a very clearly defined and well-documented method. We have clarified - with precision - the kinds of variables that are acceptable (as arguments), we've clarified the kinds of things that can be returned from the method (in this case, either a boolean or a string), and we've also added clear documentation that explains what the method does - though in a rather formal way.

If you're not familiar with this type of syntax, it may look a little bit scary at first. Fear not! The good news is that none of this is going to break your existing code. Best of all, you do not have to change your existing coding style at all. Personally speaking, I have no plans to change my coding style. The inclusion of these new elements within the framework is internal and does not change the coding requirements or functionality of Trongate in any way. For a very long time, this upgrade was put off over concerns that it would make the framework slower. What we've now learned is that since PHP 7, type hinting and return types can lead to slight performance improvements because the PHP engine can optimize operations by reducing type-related overhead. In short, this means that yesterday's upgrade should not negatively affect Trongate's benchmarks. Any performance change that does happen (whether positive or negative) will be insignificant.

The Benefits Of All That Extra Code

I'll refrain from dishing out a full tutorial on doc blocks, type hinting, return types, and access modifiers. It's probably enough to simply say that these are all elements that can be optionally added to functions and methods. Their addition makes code easier to understand and more strict. That's good for security! For example, if you have declared (via type hinting) that you expect an argument to be of the type 'integer' and an argument gets passed in that is not an integer, an error will be thrown. The case could be made that all of these factors help to make code more robust and less prone to malicious exploits.

Yesterday's upgrade also gave us an opportunity to clean up a few syntax inconsistencies that previously existed within the Trongate framework. A few functions have been moved around. We have a few new features too. However, the main thing is that we now have a framework that is built upon a consistent, modern, and future-proof codebase.

I like the sound of that. How about you?

Keep breaking the rules!

DC