Skip to content

Commit

Permalink
Improve readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Sevavietl committed Nov 3, 2018
1 parent 43bd27b commit e293f9f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@
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).

![downcasting](images/downcasting.gif)
Binary file added images/downcasting.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e293f9f

Please sign in to comment.