PHP 7: Scalar Type Declarations - New Feature!

Pubblicato il: 11 febbraio 2018
sul canale di: SevenEcks
395
3

Scalar Type Declarations

Watch my video on Scalar Variables:    • What is a Scalar Variable?  

My video on return type declarations:    • PHP 7: Return Type Declarations - New Feat...  

1. Introduced in php 5.6 as type hints and having undergone some changes, in PHP 7 they are now declarations
Type declarations allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.
2. Types of Scalar: INT, FLOAT, STRING, BOOL
Aliases for the above scalar types are not supported. So use bool, not boolean, or you'll have an error or it'll be treated as an instance of the Boolean class.
If you want more information on Scalar variables, check out my video What are Scalar Variables?
3. PHP has weak typing. By default PHP will coerce scalar values from one type to another
if it can't, IE: bool but expecting array, a TypeError will be thrown
4. Strict typing can be turned on, on a file by file basis
the exception to this is that an integer can be passed to a declaration expecting a float
To enable strict mode, the declare statement is used with the strict_types declaration:
turning on strict mode enables it for return type declartions as well
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.

More Info on Scalar Type Declarations: https://secure.php.net/manual/en/migr...


In questa pagina del sito puoi guardare il video online PHP 7: Scalar Type Declarations - New Feature! della durata di ore minuti seconda in buona qualità , che l'utente ha caricato SevenEcks 11 febbraio 2018, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 395 volte e gli è piaciuto 3 spettatori. Buona visione!