klom303 最近的时间轴更新
klom303

klom303

V2EX 第 192163 号会员,加入于 2016-09-18 10:57:50 +08:00
klom303 最近回复了
2017-03-13 12:28:15 +08:00
回复了 MrMike 创建的主题 PHP 如何优化 doctrine 的查询速度?
可以先建立一个临时表,把你的数据丢临时表里面,再用 sql 语句做比对
2017-02-06 18:01:22 +08:00
回复了 mokeyjay 创建的主题 PHP 各位用 Composer 装一个包大概需要多长时间?
我是用的国内 composer 镜像,一般不大的话几十秒的样子,可以按 https://pkg.phpcomposer.com/ 这里设置数据源。
2017-01-16 10:25:27 +08:00
回复了 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);
```
2017-01-09 09:56:43 +08:00
回复了 nolo 创建的主题 PHP 如何用 php 实现类似微信红包过期自动退回的功能
@yuanchao 你说的对,我只是觉得这样可能管理起 crontab 来会简单点
2017-01-09 09:47:49 +08:00
回复了 nolo 创建的主题 PHP 如何用 php 实现类似微信红包过期自动退回的功能
突然想到 Laravel 框架里有个任务调度器 https://laravel-china.org/docs/5.1/scheduling
尝试下 Laravel 框架,看看源码
2016-09-18 16:34:19 +08:00
回复了 dangyuluo 创建的主题 PHP PHP 如何优雅地实现类中变量的懒初始化( Initialized when visited)
第一次回复,这种懒加载可以用闭包哦

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 了,不对的话请指教
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3190 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 13:21 · PVG 21:21 · LAX 06:21 · JFK 09:21
Developed with CodeLauncher
♥ Do have faith in what you're doing.