Created
March 24, 2014 23:16
-
-
Save lividgreen/9751394 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Animal {} | |
class Cat extends Animal {} | |
class Dog extends Animal {} | |
class Manul {} | |
$c = new Cat; | |
$d = new Dog; | |
// magic begins | |
use Codegyre\Implicit; | |
Implicit::setValue($cat); // set implicit value | |
function pogladit($animal = null) { | |
$animal = Implicit::get('Animal', $animal); // get implicit value if not set explicitly | |
// ... | |
} | |
pogladit(); // implicit cat | |
pogladit($c); // explicit cat | |
pogladit($d); // explicit dog | |
// supermagic | |
$fromCatToManul = function (Cat $cat) { | |
return new Manul; | |
}; | |
Implicit::setConversion('Cat', 'Manul', fromCatToManul); | |
function pogladKotaSuka($manul = null) { | |
$manul = Implicit::get('Manul', $manul); | |
// ... | |
} | |
pogladKotaSuka($c); // Cat will be implicitly converted to Manul |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment