Skip to main content

Parallels of typing with PHP and TypeScript

Published on

I was listening to the Whiskey Web and Whatnot episode TypeScript, React, and API Issues with Matt Pocock. It made me think about the parallels between PHP and JavaScript/TypeScript typing. If you aren't familiar with TypeScript, it is JavaScript with added types. PHP is loosely typed, just like JavaScript. But PHP also allows adding type specificity to become more strictly typed, whereas JavaScript does not. Personally, I love typing my code as it reduces bugs and extra validation that I'd normally have to write. Not everyone is a fan of having to use types, and that's true across PHP and JavaScript developers. And honestly, that is okay.

The guest Matt Pocock said something that resonated with me at 24:46 in the podcast.

Actually there's quite a lot of projects out there that have a core in TypeScript to kind of like, their utilities or their API methods or things like that, or the things that fetch the database. And then if, if you think of an application like a tree, you know, you've got this stuff at the center of the tree, and then the leaves actually still in JavaScript, the components that consume that stuff. So you don't need to go all in on TypeScript in order to get the benefits of it, you don't even need to use TypeScript.

With the release of Symfony 6, the project bumped its minimum PHP dependency to 8.1 and took advantage of new language-level features. This included adding types throughout the code base. It caused some churn for end-users who extended contracts provided by Symfony. But it made using the package more stable while minimally affecting the amount of typing an end-user has to care about. This relates to Matt's comments about the "center of the tree" (your dependency components) and "the leaves" (your code using the components.)

Just because the PHP language adds more typed features, you don't need to adopt them in your application. PHP isn't forcing developers to write strictly typed code. 

An example of loosely and strictly typed with PHP

What does loosely typed mean? That means you can write code without specifying what type of value a variable is. Take the following example. The function wouldn't error with any non-string $foo and would only error if PHP cannot convert the $foo value into a string.

<?php

function example($foo) 
{
    print $foo;
}

// Prints ``, nothing.
example(null);

// Prints `1`, boolean is cast to integer and cast to a string.
example(true);

// Prints `0`
example(0);

// Fatal error: Object of class stdClass could not be converted to string.
example(new \stdClass);

PHP would throw a fatal error on any non-string value if we strictly typed our $foo value in the example function.

<?php

function example(string $foo)
{
    print $foo;
}

// Fatal error: Uncaught TypeError: example(): Argument #1 ($foo) must be of type string, null given
example(null);

You can try these on 3v4l: loosely typed and strictly typed.

You can also enforce strict types by setting declare(strict_types=1); at the top of your file. This enforces types from called code and is enabled on a file-per-file basis. Here is a basic example of passing a boolean to trim, which should only accept a string.

<?php

function example($foo)
{
    print trim($foo);
}


// No error! Prints an empty string.
example(false);

When we add declare(strict_types=1);, there will be a fatal error on passing a non-string value to trim.

<?php

declare(strict_types=1);

function example($foo)
{
    print trim($foo);
}

// Fatal error:  Uncaught TypeError: trim(): Argument #1 ($string) must be of type string, bool given.
example(false);

You can try this out on 3v4l: https://3v4l.org/nBel1#v8.2.9

 

#