DEV Community

Discussion on: How PHP Type Declarations Actually Work

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
 
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
 
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.