smknstd / modern-php-cheatsheet

Cheatsheet for some PHP knowledge you will frequently encounter in modern projects.
https://smknstd.github.io/modern-php-cheatsheet/
MIT License
1.28k stars 108 forks source link

Use type declarations instead of type hint #4

Closed Girgias closed 3 years ago

Girgias commented 3 years ago

The PHP engine enforces the types at runtime, therefore they aren't merely just hints but actual type declarations.

smknstd commented 3 years ago

thanx for the suggestion. Just rephrase that section, what do you think ? Is it any better ?

Girgias commented 3 years ago

A couple of nits, it's not really an expected data type, as in coercive typing mode you can pass an integer to a string type declaration or an integer-string to an int type declaration (well some caveats on the latter case).

Also technically you could use parameter types for array and class-types prior to PHP 7.

For void "should not return" is incorrect, "does not return a value" is more correct (although PHP will always assign a return value to a function, but that's internal details and not an API indication), as never (PHP 8.1) fills this niche for when a function does not return (by always throwing an exception, calling exit/die, or being an infinite loop)

Finally intersection types are missing from it, although that's PHP 8.1 only. :-)

smknstd commented 3 years ago

Thanx for sharing !

Just found out about the strict mode : https://twitter.com/enunomaduro/status/1452568654420267008

I didn't know about those new 8.1 features. Feel free to make a PR as it will be out soon !