Previous version of PHP allows object and array type hinting. PHP7 has an ability to type hint scalar type (primitive type) such as integer, boolean and string. For e.g.,
//define function with primitive type hinting function myAge(int $age) { var_dump($age); } //will work myAge(66); //will also work myAge('66'); //will fail myAge('sixty six');
In above, example when calling function myAge, integer has to be passed as a parameter. It is strict but not entirely strict. This is because - instead of 66
if you pass '66'
, PHP will try its best to translate it to 66. However if you pass string 'sixty six'
as a parameter, php will throw an error.
If you want PHP to be entirely strict, i.e to accept integer 66
and not '66'
, you can declare strict_types=1
at the beginning as shown below:
declare(strict_types=1); function myAge(int $age) { var_dump($age); } //will work myAge(66); //will throw an error myAge('66');
We can also now type hint return values in PHP 7. For example, say we have getCustomer() function and we expect customer instance to be returned. We would normally do it as :
class Customer {} function getCustomer() { return new Customer; } var_dump(getCustomer()); //get customer object
However, in above example there is no protection against returning array instead of object. For e.g. one can easily do as below, and PHP won't complain.
class Customer {} function getCustomer() { return [ ]; }
If you want this function to be more strict regarding what it should be returning, you can add return type hint as below.
class Customer {} function getCustomer() : Customer { return new Customer; }
Now, if you return array instead of Customer object PHP will blow up.
This feature could be really useful in defining interfaces where other people will create a concrete implementation. Example is shown below:
class Customer {} interface CustomerInterface { public class getCustomer() : Customer; } class SomeClass implements CustomerInterface { public function getCustomer() : Customer { return new Customer; } } (new SomeClass)->getCustomer();
In practice, you don't need to return types for everything but only do it in a situation when it is very important that the people who use interface adhere precisely to the contract.
Three-way comparision operator, also known as the "spaceship operator" <=>
is available to use in PHP 7. It can be used for combined comparisons - mostly when dealing and sorting.
Below are some examples of its behaviour:
echo 1 <=> 1; //0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Floats echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // Strings echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1 echo "a" <=> "aa"; // -1 echo "zz" <=> "aa"; // 1 // Arrays echo [] <=> []; // 0 echo [1, 2, 3] <=> [1, 2, 3]; // 0 echo [1, 2, 3] <=> []; // 1 echo [1, 2, 3] <=> [1, 2, 1]; // 1 echo [1, 2, 3] <=> [1, 2, 4]; // -1 // Objects $a = (object) ["a" => "b"]; $b = (object) ["a" => "b"]; echo $a <=> $b; // 0 $a = (object) ["a" => "b"]; $b = (object) ["a" => "c"]; echo $a <=> $b; // -1 $a = (object) ["a" => "c"]; $b = (object) ["a" => "b"]; echo $a <=> $b; // 1 // only values are compared $a = (object) ["a" => "b"]; $b = (object) ["b" => "b"]; echo $a <=> $b; // 0