Introducing strong native PHP types

Matt Kingshott 👨🏻‍💻 on April 30, 2019

Today, after around a month of hacking, I’m pleased to announce my attempt at addressing PHP’s often-criticised weak type system. Some things you... [Read Full]
markdown guide

Great effort! And I've just added a star from my side. :-) :-) I like that you've also made an attempt at cleaning up the mess there is.

In other words, this isn’t a PHP extension or custom version of PHP itself

And that is the best approach, in my opinion. The last thing we want is another competing flavor or an extension that no one can seem to install. :-D

(The code in this post doesn't have syntax highlighting; maybe you missed adding the "php" part to the Markdown?)


Thanks @ankush981 !

The post was a crosspost from Medium and it didn't add the PHP markdown. I've updated the post to use the PHP syntax highlighting.


why did you choose to have the mutable option? It looks like that you can do everything you need with the immutable version, and get more type safety


Thanks for the reply.

I’m not sure I understand your question. When you make a type immutable, you can’t change its value. So, utility methods will trigger an exception. All you can do is change its type.

If a type is mutable, you can modify its value, as well as convert it to another type.


The point was not the meaning of mutability/immutability. The point was that mutability makes the whole typing weaker, and IMHO should be avoided if possible.

It would be safer to have everything immutable and just return new values (with clone) instead of modifying the current ones

After further thought, I agree and have released an updated version (2.0) that makes all utility methods return new instances, thus providing immutability.


I love the idea of strong types and appreciate the effort here! This is a similar approach to how Java handles types. But you're taking native primitive types and wrapping them in your own logic. What benefit is it to me to use this library of Types rather than just the primitive versions?


Thanks for the reply.

The primary motivation for using it is to prevent type coercion, enforce whether null is permitted or not, and gain access to a readable, fluent API for interacting with data (see the examples).


Wow, really nice. Utility methods bought me completely. Gonna star and wish you good luck in further development. 👌

code of conduct - report abuse