DEV Community

Cover image for How PHP Type Declarations Actually Work

How PHP Type Declarations Actually Work

Rob Waller on April 19, 2018

Type declarations are a simple programming concept that lots of developers use on a daily basis. In the code example below we see a basic method th...
Collapse
 
wilburpowery profile image
Wilbur Powery

Even though I sometime use type declarations, I try to prevent myself to start using them in every single scenario. An Example of where I mostly reach for type declarations is this:

...
public function addEvent(array $data) : Event
{
   return Event::create($data);
}
Enter fullscreen mode Exit fullscreen mode

I think in cases like the top one it helps for readability.

I feel sometimes that when I reach for so much type hints and return types, I'm making PHP something more Like Java (And I really don't enjoy Java).

It's just my way of thinking. Totally respect your point of view also. 😊

Collapse
 
robdwaller profile image
Rob Waller

I think this often depends on your code background. I got into PHP after I'd written more strongly typed languages like c#. So I've always felt comfortable around type hints, etc.

Collapse
 
wilburpowery profile image
Wilbur Powery

Absolutely agree with you.

Collapse
 
wesnetmo profile image
Wes • Edited

then how can you tell what to pass to functions?

$order->pay(new Potato());
Collapse
 
wilburpowery profile image
Wilbur Powery

lol, your talking like the developer is a kid. I’m expected to have a certain domain over the codebase. 😁

Thread Thread
 
wesnetmo profile image
Wes

Until you don't.

Thread Thread
 
jochemstoel profile image
Jochem Stoel

This is debatable.

Collapse
 
alexhaxe profile image
Alexander Blum

Have you looked into using Haxe as a possible way to have strict typing while still being able to target PHP?
Granted type checking only happens during compile time and generated PHP code might be harder to read, but Haxe adds a lot more than just type checking.

So if you've never heard of Haxe, it might be worth checking it out.

Disclaimer: Despite my user name, I'm not connected to the makers of Haxe, but I'm using Haxe for my past, current and future PHP projects.

Collapse
 
robdwaller profile image
Rob Waller

Thanks for the suggestion I'll give it a look.

Collapse
 
clabinger profile image
Cooper Labinger • Edited

I think you got the per-file basis backwards: strict types must be declared in the file calling the function, not the file where the function is defined.

From the PHP manual:

Strict typing applies to function calls made from within the file with strict typing enabled, not to the functions declared within that file. If a file without strict typing enabled makes a call to a function that was defined in a file with strict typing, the caller's preference (weak typing) will be respected, and the value will be coerced.

See php.net/manual/en/functions.argume...

Sadly, I think this partly defeats the purpose of enforcing types, since having to remember to declare strict types in each file that calls a function is not that different from just getting the function calls right in the first place.

Collapse
 
robdwaller profile image
Rob Waller

Yes what the manual says is sort of right on parameter types. If you don't declare strict types then the calling code will fall back to type coercion and not impose the parameter types defined in the required file. But if it can't coerce to the defined parameter type the code will still fail.

I've updated the associated GitHub library to highlight this point. github.com/RobDWaller/type-declara...

The article itself though is still technically correct it's just it is focused on return types and the fact you can't trust what you receive from a method without strict types imposed. I should maybe have stressed this point more.

I agree though that PHP Types aren't ideal but do believe they still have value when used correctly, you just have to impose strict types everywhere. 😂

Collapse
 
andreidascalu profile image
Andrei Dascalu

"This approach does have some consequences though." - it's not this approach though.

Type coercion is what PHP always does by default. Basically, to make it simple, strict types means PHP won't do coercion when types are declared and will perform the default coercion behaviour otherwise.