https://github.com/thoroughphp/typecasting
https://github.com/thoroughphp/typecasting
downcast php typecast types
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/thoroughphp/typecasting
- Owner: ThoroughPHP
- License: mit
- Created: 2018-11-01T20:03:25.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-03T11:45:26.000Z (over 7 years ago)
- Last Synced: 2025-07-14T14:45:05.493Z (12 months ago)
- Topics: downcast, php, typecast, types
- Language: PHP
- Size: 427 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeCasting - kind of explicit type casting in PHP
[](https://travis-ci.com/Sevavietl/TypeCasting)
[](https://coveralls.io/github/Sevavietl/TypeCasting)
[](https://opensource.org/licenses/MIT)
[](https://github.com/phpstan/phpstan)
Neither real [covariance](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)) in method return type hints, nor [explicit type casting](https://www.baeldung.com/java-type-casting) are supported in PHP.
Sometimes it is useful to downcast object, so PhpStorm or PHPStan stop complaining about absent methods or mismatched types.
Usually [@var](http://docs.phpdoc.org/references/phpdoc/tags/var.html) tag used for this. But as with all phpdoc tags in regard of typing, this is simply telling your IDE or static analyzer tool to take your honest word that you know for sure the type of the stuff.
Whereas with actual type casting (like say in java, C# or even in PHP, but only for primitives) you can be sure that object (or primitive) is "castable" into specified type, or end up with exception.
So you can plug `\TypeCasting\TypeCasting` trait into your class (but only one per inheritance chain) and then use `cast` method that is a bit like [`Java.lang.Class.cast`](https://www.tutorialspoint.com/java/lang/class_cast.htm) method (but of course this is oversimplified comparison).
