PHP Strict Types and Type-Hinting Cheat Sheet

Everbody's doing this BRAND NEW thing! Write stricter, more readable PHP!

So, PHP has this totally new, super recent thing, a sorta strict mode and type hinting! It's really, really new and I've finally got into it. By new I mean it only rolled out about a decade ago.

Anywho, something I didn't really find was a sorta cheat sheet for those who like me have PHP experience but have only recently discovered this stuff. So here it is! I hope it helps you with your PHP endeavours.

Please, no complaining in the comments that I don't use private or public. :) Well, you can if you want, but I won't listen. xD

Note: I am currently adding stuff to this as I discover it.

<?php // declare this right at the start (after the <?php) in each php file // where you want strict type hinting // // with this, you'll get an error, for example, if you try and assign an // int a string or a string an int, etc. includes() are NOT affected. it // applies to the file, simple as that declare( strict_types = 1 ); // type hinting in "globals" // ========================= // these... WON'T work! you can't make "global" variables with type // hinting. none of these work - you'll get an error string $myStr = ""; int $myInt = 1; float $myFloat = 1.0; array $myArray = []; // type hinting in functions // ========================= // void function (notice :void at the end) which takes type hinted params function doSomeStuff( string $somethingToSay, int $anInt, float $aFloat, array $anArray ): void { // these are ok! echo "Say:" . $somethingToSay; $anIntPlusOne = $anInt + 1; $aFloatPlusOnePoint5 = $aFloat + 1.5; array_push( $anArray, "aye aye capn" ); // these are not and will give an error $somethingToSay = 1; // nope, can't set string to int $anIntPlusOne = "An int plus one"; // nope, can't int to string $aFloatPlusOnePoint5 = []; // nope. can't make a float an array $anArray = "banana"; // nope. can't make an array a string } // example of function which returns a string function gimmeString():string { return "a string!"; // ok return 1; // nope. we specified the function returns string } // example of function which returns an int function gimmeInt():int { return 1; // ok return "one"; // nope. we specified the function returns int } // example of function which returns a float function gimmeFloat():float { return 1.0; // ok, 1.0 is a valid float return 1; // ok, since 1 is 1.0 return "one point oh"; // nope } // example of function which returns an array function gimmeArray():array { return [ 1, 2, 3 ]; // ok. valid array return 1.0; // nope. we specified function returns an array } // type hinting local variables... // =============================== function localVarTypeHinting() { // you can't dewit, sorry, none of these will work. apparently they're // considering it for php 9 string $myStr = "hi"; // nope int $myInt = 1; // nope float $myFloat = 1.0; // nope array $myArray = []; // nope } // type hinting in classes // ======================= class MyClass { // type hinting in properties // -------------------------- // class properties can be type hinted. you could swap var for public // or private etc if you really want. but i don't want var string myStr = "cat"; var int myInt = 1; var float myFloat = 1.0; var array myArray = [ 1, 2, 3 ]; // type hinting in methods // ----------------------- // example of stricty, type hinty method parameters function setStuff( string myStr_, int myInt_, float myFloat_, array myArray_ ):void { // ok $this->myStr = myStr_; $this->myInt = myInt_; $this->myFloat = myFloat_; $this->myArray = myArray; // not ok $this->myStr = 3; $this->myInt = "three"; $this->myFloat = "one point 5"; $this->myArray = 1; } // example of method which returns a string function gimmeString():string { return "a string!"; // ok return 1; // nope. we specified the method returns string } // example of method which returns an int function gimmeInt():int { return 1; // ok return "one"; // nope. we specified the method returns int } // example of method which returns a float function gimmeFloat():float { return 1.0; // ok, 1.0 is a valid float return 1; // ok, since 1 is 1.0 return "one point oh"; // nope } // example of method which returns an array function gimmeArray():array { return [ 1, 2, 3 ]; // ok. valid array return 1.0; // nope. we specified method returns an array } } ?>
Comments? Visit the Guestbook!