Download the PHP package sj-i/php-cast without Composer
On this page you can find all versions of the php package sj-i/php-cast. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Table of contents
Download sj-i/php-cast
More information about sj-i/php-cast
Files in sj-i/php-cast
Download sj-i/php-cast
More information about sj-i/php-cast
Files in sj-i/php-cast
Please rate this library. Is it a good library?
Informations about the package php-cast
CAST IN THE NAME OF PHP, YE NOT GUILTY
ABOUT
- You can cast any values in the weak mode manner, even if you are in the strict mode (strict_types=1).
INSTALL
SUPPORTED VERSIONS
- PHP 7.0+
USAGE
HOW IT WORKS
- Return type declarations in weak mode files do the job.
PhpCast\Cast
is defined in a file declared asstrict_types=0
- Though
strict_types=0
is the current default of PHP, it is explicitly declared to assert the intent.
- Though
- The type checks for parameters are done in the caller mode, but the type checks for return values are always done in the callee mode.
"WHY WEAK MODE? I WANT TO USE STRICT MODE IN EVERYWHERE!"
- To use
strict_types=1
without anystrict_types=0
codes, explicit casts have to be used for untyped data from external sources like DB or HTTP requests. - But in PHP, explicit cast like
(int)$foo
never fails with a few exceptions.(int)'abc'
silently results to0
and doesn't emit a warning.
- So some validations are necessary before just using casts.
- If explicit casts are used without proper validations by a lazy programmer, the purpose of the type declarations are totally ruined.
- This was, if I understand it correctly, one of the reasons that strict type checks weren't totally welcomed with open arms during the discussion introducing the scalar type hinting to PHP.
- Defining proper rules of validations and type conversions is sometimes overkill.
- Even though defining them are always "correct" things to do, in humans life, there are times you don't need to be so correct.
- Let's cast untyped values in the weak mode manner, use the "official" rules of validations and type conversions in the PHP world!
- No need to invent and learn new rules every day every night everywhere.
- If the "official" rules doesn't fit your needs, then define and use your own rules selectively there.
- If you've ever read the accepted RFC of STH, you would notice the use of weak mode like this library has been already mentioned there.
This proposal is not a compromise. It is an attempt of allowing strict typing to work in PHP. A mechanism to bridge untyped PHP code with strict typed PHP code, a “weak” bridge, would be required (otherwise explicit (type) casts would be needed). This proposal unifies the strict and weak typing into a single system that integrates tightly and behaves consistently.
LICENSE
MIT
All versions of php-cast with dependencies
PHP Build Version
Package Version
Requires
php Version
^7.0 || ^8.0
The package sj-i/php-cast contains the following files
Loading the files please wait ....