ThoroughPHP / TypeCasting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeCasting - kind of explicit type casting in PHP

Build Status Coverage Status License: MIT PHPStan

Neither real covariance in method return type hints, nor explicit 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 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 method (but of course this is oversimplified comparison).

downcasting

About

License:MIT License


Languages

Language:PHP 100.0%