klom303's recent timeline updates
klom303

klom303

V2EX member #192163, joined on 2016-09-18 10:57:50 +08:00
klom303's recent replies
Mar 13, 2017
Replied to a topic by MrMike PHP 如何优化 doctrine 的查询速度?
可以先建立一个临时表,把你的数据丢临时表里面,再用 sql 语句做比对
Feb 6, 2017
Replied to a topic by mokeyjay PHP 各位用 Composer 装一个包大概需要多长时间?
我是用的国内 composer 镜像,一般不大的话几十秒的样子,可以按 https://pkg.phpcomposer.com/ 这里设置数据源。
Jan 16, 2017
Replied to a topic by koodai PHP PHP 的数组定义了键名,就不能使用索引了吗?
其实要处理数组,不一定要套 for 循环,有一些很好用的数组函数啊,比如:
```
$data = array(
'filed1' => array(1, 2, 3),
'filed2' => array('a', 'b', 'c'),
'filed3' => array('x', 'y', 'z'),
);

$result = array_map(function ($filed1, $filed2, $filed3) {
return "('{$filed1}','{$filed2}','{$filed3}')";
}, $data['filed1'], $data['filed2'], $data['filed3']);

echo implode(',',$result);
```
@yuanchao 你说的对,我只是觉得这样可能管理起 crontab 来会简单点
突然想到 Laravel 框架里有个任务调度器 https://laravel-china.org/docs/5.1/scheduling
尝试下 Laravel 框架,看看源码
第一次回复,这种懒加载可以用闭包哦

class DB
{
private $binds = [];
private $instances = [];

public function __construct()
{
$this->bind('a', function ($config) {
return new A($config);
});
$this->bind('b', function ($config) {
return new B($config);
});
}

private function bind($key, Closure $closure)
{
if (!isset($this->binds[$key])) {
$this->binds[$key] = $closure;
}
}

public function make($key, array $params)
{
if (isset($this->instances[$key])) {
return $this->instances[$key];
}
if (!isset($this->binds[$key])) {
return false;
}
$this->instances[$key] = call_user_func_array($this->binds[$key], $params);
return $this->instances[$key];
}
}

class A
{
private $config;

public function __construct($config)
{
$this->config = $config;
}

public function foo()
{
echo "Class A:\n";
var_dump($this->config);
}
}

class B
{
private $config;

public function __construct($config)
{
$this->config = $config;
}

public function foo()
{
echo "Class B:\n";
var_dump($this->config);
}
}

$db = new DB();
$a = $db->make('a', ['config for a']);
$a->foo();
$b = $db->make('b', ['config for b']);
$b->foo();

这确实是单例+依赖注入的问题
DB 类一般是要做单例的,这里只是写个例子就直接 new 了,不对的话请指教
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   951 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 20:16 · PVG 04:16 · LAX 13:16 · JFK 16:16
♥ Do have faith in what you're doing.