Created
January 14, 2016 21:21
-
-
Save tomphp/6599572250676c976e92 to your computer and use it in GitHub Desktop.
Example of tomphp/transform __() function
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 | |
// https://github.com/tomphp/php-transform | |
use function TomPHP\Transform\__; | |
$ticket1 = new Ticket(new User(['dob' => new DateTime('1980-01-22')])); | |
$ticket2 = new Ticket(new User(['dob' => new DateTime('1984-08-08')])); | |
$tickets = [$ticket1, $ticket2]; | |
// Long winded approach | |
$dobs1 = array_map( | |
function (Ticket $ticket) { | |
return $ticket->getUser()->info['dob']->format('Y-m-d'); | |
}, | |
$tickets | |
); | |
var_dump($dobs1); | |
// Magic approach!!! | |
$dobs2 = array_map(__()->getUser()->info['dob']->format('Y-m-d'), $tickets); | |
var_dump($dobs2); | |
/*****************************************************************************/ | |
class User | |
{ | |
public $info; | |
/** | |
* @param array $info | |
*/ | |
public function __construct(array $info) | |
{ | |
$this->info = $info; | |
} | |
} | |
class Ticket | |
{ | |
/** @var User */ | |
private $user; | |
/** | |
* @param User $user | |
*/ | |
public function __construct(User $user) | |
{ | |
$this->user = $user; | |
} | |
public function getUser() | |
{ | |
return $this->user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment