Skip to content

Instantly share code, notes, and snippets.

@lividgreen
Created March 24, 2014 23:16
Show Gist options
  • Save lividgreen/9751394 to your computer and use it in GitHub Desktop.
Save lividgreen/9751394 to your computer and use it in GitHub Desktop.
<?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