sevavietl/type-casting

v0.1 2018-11-03 11:43 UTC

This package is not auto-updated.

Last update: 2024-05-27 19:17:02 UTC


README

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