Skip to content

ThoroughPHP/TypeCasting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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