PHP code example of uuf6429 / php-castable

1. Go to this page and download the library: Download uuf6429/php-castable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

uuf6429 / php-castable example snippets


class Cat implements \uuf6429\Castable\Castable
{
    public function castTo($type)
    {
        if ($type === Dog::class) {
            return new Dog();
        }

        throw new RuntimeException("Unsupported type $type.");
    }
}

class Dog {}

$dog = \uuf6429\Castable\cast(new Cat(), Dog::class); // ok, cat becomes a dog :)
$cat = \uuf6429\Castable\cast($dog, Cat::class);      // not allowed
shell
composer