I'm pleased to declare that the process of adding type declarations (sometimes called 'type hinting') and return types is now underway for the Trongate framework. At the time of writing this is NOT a breaking change. We remain v1 forever. So long as there are no glitches, this should not break any of your pre-existing code.

What Are Type Declarations And Return Types?

If you don't know what type declarations (or type hinting) or return types means then don't worry. I'll be putting out a YouTube tutorial shortly. In the meantime, I can tell you that this is (at the time of writing) an optional feature that was added to PHP in version 7. Unless I'm very much mistaken, it was added in version 7.2. To understand type declarations, it's probably best to think about the basic structure of a PHP function. Consider the following PHP function:

function allowed_to_drive($age) {
  if ($age>=18) {
    return true;
  } else {
    return false;
  }
}
This function accepts an argument called 'age' and returns a boolean of either true or false. With more recent versions of PHP, it's possible to clarify what kind (or type) of variable we expect our arguments to be. In this instance, we expect $age to be an integer. We can also declare what we expect our function to return. In our case, we expect our function to return a boolean (i.e., either true or false). This means that we can improve our function by adding type declarations (sometimes called 'type hinting') and also a 'return type' declaration. This would leave us with:

function allowed_to_drive(int $age): bool {
  if ($age>=18) {
    return true;
  } else {
    return false;
  }
}
In the function above, we've made two changes. As you can see, we've now declared that $age should be an integer (i.e., 'int'). Also, we've added : bool before the opening curly brackets. This means that our function should return a boolean value of either true or false.

Why has this been added?

The absolute highest priority for Trongate is stability. Our goal is to build a PHP framework that can let you build an app, launch it and leave it online for ten years. There is no other framework - of any language - that prioritises stability as much as Trongate. It's our calling card. Stability is what we believe in. For us, Trongate means stability. This cannot be stressed enough. However, there are two reasons why adding return types makes sense: Firstly, return types - and type hinting - are good for error prevention. It gives developers an opportunity to look at code and - at a glance - they'll be given an indication of what arguments a function can be expected to accept and output. This type of code also has the potential to be picked up by modern IDEs, giving developers the opportunity to have code suggestions appear on the screen as they type. Secondly, there appears to be a general trend - across the wider web development community - that is driving developers towards a more strict type of coding. Sometimes, when people speak about this kind of thing you might hear phrases like 'strict type setting'. It all sounds a little bit scary but don't worry, in this instance 'strict' is actually a good thing. This is something that we can add to our code to make our code a little bit less prone to errors. That's all. Finally, on a slightly more personal note, I've observed that the PHP Foundation appear to be increasingly eager to change and break PHP frequently. What I'm about to say is pure speculation but I think it's only a matter of time until type declarations and return types become compulsory in future versions of PHP. If I'm right then it makes sense for us to stay ahead of the crowd by adding this to our framework ahead of time. If I'm wrong then at the very least it gives all of us an opportunity to familiarise ourselves with a style of coding that's more in alignment with the type of syntax that we see with languages like; C, C++, Rust (I think!), Go and Kotlin. That's healthy. It's good!

The Bigger Picture

I've had a look at the current roster of popular PHP frameworks. As far as I can tell, Symfony was the first to embrace return types and type declarations. I've found one or two frameworks that have gone 'half way house'. However, apart from Symfony, I'm not aware of any other PHP frameworks that have gone 'all in' for strict type setting. As of today, Trongate now stands shoulder to shoulder with Symfony as a framework that embraces strict type setting. Some of you might be a little bit shocked to hear this but I suppose you could say, modern PHP syntax. The Trongate naysayers, who have often confused 'v1 forever' as meaning 'old fashioned', are going to have to find something else to complain about! At the time of writing, type setting has been added to the main Trongate.php class. Adding this to the rest of our engine (and our eco-system) is a process that may take several months. However, that process has now officially started. In the weeks ahead, there is a moderate chance of faults and we may not get things right first time. However, there appears to be no question that strict types are the future of not just PHP but web development generally. So, the sooner we make this switch, the better.

What to expect, moving forward

In the weeks ahead, the entire 'engine' directory, along with the modules that come with Trongate are going to be getting updated so that they use type declarations and return types. On a slightly more personal note, I'll be posting a return types tutorial on YouTube so that everyone has an opportunity to get to grips with this update. Also, all of my future modules and tutorials (either on YouTube or Speed Coding Academy) will be using return types.

Don't worry - be happy

Never forget, every single decision being made - with regards to Trongate - is being made with stability as a priority. Our goal is to be v1 forever. This does NOT mean that things never break. Sometimes things will break - either because of changes with the PHP language or because of stupid errors. Nevertheless, I want to reassure you that Trongate is absolutely committed to stability. Remember, this should not be a breaking change for anyone. If any of this seems confusing or scary then do not worry and don't panic. At this stage, it's all optional and our goal here is to create a framework for the future. A framework that is built to last. I want to send a message to the decision makers in large corporations. They must know that there's a new PHP framework that prioritises stability. When CEOs and IT managers choose Trongate, they must sleep comfortably at night, knowing that they're running with a framework that has no single points of failure, no management by committee, no third-party dependences and definitely no pointless updates. The vision that Team Trongate have is crystal clear. We want to give the web development community and the business community a framework that's fast, easy to learn and incredibly stable. That's the dream! Happy coding, DC