/** * @return string 获取文本内容 */ public function getTextContent() { $self = $this; return Lazy::init($this->_textContent, function () use($self) { return file_get_contents($self->path); }); }
public function getTitle() { return Lazy::init($this->_title, function () { $textContent = $this->getTextContent(); foreach (explode("\n", $textContent) as $line) { $line = trim($line); if ($line[0] != '#') { return ltrim($line, '*'); } } return $this->name; }); }
}); break; case "itersum": test(function () use($add1, $sum, $dummyFunc) { $result = 0; $tmp = null; for ($i = 0; $i <= ITER_MAX; $i++) { // $result += $i + 1; $result += $add1($i); $tmp = $dummyFunc($tmp); } echo "iter result: " . $result . PHP_EOL; }); break; case "lazyfile": test(function () use($existSql) { Lazy::fromFile("db.log")->filter($existSql)->toFile("db_filter.log"); }); break; case "memfile": test(function () use($existSql) { $buffer = []; foreach (file("db.log") as $v) { if ($existSql) { $buffer[] = $v; } } file_put_contents("db_filter.log", implode($buffer)); }); break; }
public function __construct() { parent::__construct(); $this->addClasses($this->_loaders, __NAMESPACE__); }
/** * @return Config */ public static function instance() { return Lazy::init(self::$_instance, function () { return new Config(); }); }
/** * @return User|null 当前登录的用户;若未登录,则为空 */ public static function getCurrentLoginUser() { /**@var $webUser CWebUser */ $webUser = Yii::app()->user; if ($webUser->isGuest) { return null; } return Lazy::init(self::$_loginUser, function () use($webUser) { return User::model()->findByPk($webUser->id); }); }
/** * @return array 所有支持的文件类型 */ public static function getAvailableFileTypes() { return Lazy::init(self::$_supportedPageFileTypes, function () { return explode('|', WIKI_AVAILABLE_FILE_TYPES); }); }
})->filter(function ($v) { return $v & 1; }); echo Lazy::fromArray($input)->map(function ($v) { return $v + 1; })->filter(function ($v) { return $v & 1; })->reduce(function ($carry, $v) { return $carry + $v; }) . PHP_EOL; echo Lazy::fromArray($input)->map(function ($v) { return $v + 1; })->filter(function ($k) { return $k === "x0"; }, ARRAY_FILTER_USE_KEY); echo Lazy::fromArray($input)->map(function ($v) { return $v + 1; })->filter(function ($v, $k) { return $k === "x0" && $v === 1; }, ARRAY_FILTER_USE_BOTH); //*/ /* Lazy::fromFile("db.log")->filter(function($v) { return (strpos($v, "SQL") !== false); })->toFile("db_filter.log"); //*/ /* $ten = Lazy::fromRange(0, 9)->getIterator(); $addOne = function($v, $x1, $x2) { return $v + $x1 + $x2;}; // 迭代器的使用错误: // 同一个迭代器传入,foreach速递 *= 迭代器个数
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/12/22 * Time: 18:32 */ class Lazy { protected $_loaded = false; protected $_name; public function materialize() { $this->_loaded = true; $this->_name = 'Kevin'; } public function getName() { if (!$this->_loaded) { throw new LogicException('Call materialize() before accessing'); } return $this->_name; } } $lazy = new Lazy(); echo $lazy->getName();