Skip to content

Instantly share code, notes, and snippets.

@imliam imliam/take.php
Last active Sep 6, 2019

Embed
What would you like to do?
Run functions consecutively by piping through the result of one into the next.
<?php
if (! function_exists('take')) {
/**
* Run functions consecutively by piping through the result of one
* into the next.
*
* @param mixed $value A value
*
* @return object
*/
function take($value)
{
return new class($value) {
public function __construct($value)
{
$this->id = '__pipe-' . uniqid();
$GLOBALS[$this->id] = $value;
}
public function __destruct()
{
unset($GLOBALS[$this->id]);
}
public function pipe($value)
{
$GLOBALS[$this->id] = $value;
return $this;
}
public function __get($name)
{
if ($name === 'value') {
return $GLOBALS[$this->id];
}
trigger_error("Property '$name' doesn't exist and cannot be gotten", E_USER_ERROR);
}
};
}
}
@imliam

This comment has been minimized.

Copy link
Owner Author

commented Feb 23, 2018

Example usage:

$taken = take('HeLLo, WoRld.');

$taken->pipe(strtolower($taken->value))
      ->pipe(ucfirst($taken->value));

echo $taken->value; // 'Hello world.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.