Created
July 29, 2011 20:15
-
-
Save igorw/1114629 to your computer and use it in GitHub Desktop.
playing with PHP 5.4
This file contains hidden or 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 Container extends ArrayObject | |
{ | |
} | |
trait ContainerAware | |
{ | |
private $container; | |
public function setContainer(Container $container) | |
{ | |
$this->container = $container; | |
} | |
public function get($name) | |
{ | |
return $this->container[$name]; | |
} | |
} | |
class Guitar | |
{ | |
public function rock() | |
{ | |
return 'CRAZY SHIT GUITAR SOLO!'; | |
} | |
} | |
class Rockstar | |
{ | |
use ContainerAware; | |
public function getSongs() | |
{ | |
return [ | |
'Smoke on the water', | |
'Other shizzle', | |
]; | |
} | |
public function getOneSongLater() | |
{ | |
return function ($i) { | |
return $this->getSongs()[$i]; | |
}; | |
} | |
public function rockTheGuitar() | |
{ | |
return $this->get('guitar')->rock(); | |
} | |
} | |
$star = new Rockstar(); | |
var_dump($star->getSongs()[0]); | |
$f = $star->getOneSongLater(); | |
var_dump($f(1)); | |
$container = new Container(); | |
$container['guitar'] = new Guitar(); | |
$star->setContainer($container); | |
var_dump($star->rockTheGuitar()); |
It's an argument to the closure, allowing you to specify it later - e.g. at closure call time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you omit the $i argument for Rockstar::getOneSongLater() ?