内容简介:这篇文章用来记录开发中遇到的一些小问题的解决方案,会保持持续更新。一种奇特的调用方式,看下面的使用示例,使用类的方法变得更加灵活了。其中,
这篇文章用来记录开发中遇到的一些小问题的解决方案,会保持持续更新。
call_user_func()函数和call_user_func_array()函数
一种奇特的调用方式,看下面的使用示例,使用类的方法变得更加灵活了。
<?php
class Test
{
public function do($str)
{
echo $str . PHP_EOL;
}
}
class Stest
{
public static function do($a, $b)
{
var_dump($a + $b);
}
}
// 调用内置函数
$a = call_user_func('explode', ',', '1,2,3');
var_dump($a);
$a = call_user_func_array('explode', [',', '1,2,3']);
var_dump($a);
// 匿名函数调用
$b = call_user_func(function($x, $y){
return $x - $y;
}, 13, 9);
var_dump($b);
$b = call_user_func_array(function($x, $y){
return $x + $y;
}, [11, 22]);
var_dump($b);
$p = 10;
$b = call_user_func_array(function($x, $y) use($p){
return $x + $y + $p;
}, [11, 22]);
var_dump($b);
// 调用对象的方法
call_user_func([(new Test()), 'do'], '99999888888');
// 静态方法调用
$class = 'STest';
call_user_func([$class, 'do'], 12, 10);
call_user_func_array([$class, 'do'], [88, 10]);
// 静态方法另外一种写法
call_user_func($class . '::do', 12, 10);
call_user_func_array($class . '::do', [88, 10]);
统计富文本提交中文文本的长度:
$length = mb_strlen(strip_tags(htmlspecialchars_decode($data['content'])),'UTF8');
其中, htmlspecialchars_decode() 反转义 html 标签, strip_tags() 去掉标签, mb_strlen($str,'UTF8') 统计长度。
php判断进程是否存在,不存在则重启
public function checkQueue()
{
$cmd = 'ps axu | grep "queue" | grep -v "grep" | wc -l';
$ret = shell_exec("$cmd");
$ret = rtrim($ret, "\r\n");
if($ret === "0") {
$php_path = '/www/test/index.php'; //php脚本路径
$start_cmd = "nohup " . $php_path . " queue >> /www/test/log/queue.log 2>&1 &";
$ret = shell_exec("$start_cmd");
}
}
自动加载函数spl_autoload_register()的使用
这个函数在很多框架的自动加载机制中都有用到,了解一下。在根目录新建一个子文件夹 class ,里面有一个类文件 Dog.php :
<?php
class Dog
{
public function doSomething()
{
echo 'SAY: "Hello world!!!"';
}
}
在根目录运行 index.php :
<?php
class AutoLoader {
public static $_path = __DIR__ . '/class/';
public static function run($class) {
$file = self::$_path . $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
}
spl_autoload_register("AutoLoader::run");
// 另外一种写法 spl_autoload_register(array('AutoLoader', 'run'));
$obj = new Dog();
$obj->doSomething();
// 运行结果 SAY: "Hello world!!!"
当new一个未被引入的类时(Dog类),spl_autoload_register()会运行AutoLoader类的run方法,根据类名和事先约定好的路径,去尝试加载这个类,代码得以正常运行,这在框架中新建类的自动加载时很有用。
自动获取第一天最后一天
// 当前时间2019-04-11
echo date("Y-m-d", strtotime("2019-02 first day of")).'<br>'; // 2019-02-01
echo date("Y-m-d", strtotime("2019-02 last day of")).'<br>'; // 2019-02-28
echo date("Y-m-d", strtotime("2019-05 last day of")).'<br>'; // 2019-05-31
echo date("Y-m-d", strtotime("first day of")).'<br>'; // 2019-04-01
echo date("Y-m-d", strtotime("last day of")).'<br>'; // 2019-04-30
上面是使用 PHP 自带的时间函数来处理日期问题,处理更复杂一些的日期相关问题,强烈建议使用第三方包 Carbon 。
二维数组中某个字段求和
$arr = [
['date' => "2019-03-06", "count" => 129],
['date' => "2019-03-13", "count" => 101],
['date' => "2019-03-28", "count" => 244],
];
echo array_sum(array_column($arr, 'count'))];
// 474
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
商业模式新生代
亚历山大•奥斯特瓦德 (Alexander Osterwalder)、伊夫•皮尼厄 (Yves Pigneur) / 王帅、毛心宇、严威 / 机械工业出版社 / 2011-8-15 / 88.00元
中文官网:http://www.bizmodel.org 内容简介:当你愉快的看完第一章:商业模式画布,赫然发现这些构成要素全 都交织成一幅清晰的图像在脑海中呈现,它们如何互相影响、如何交互作用全都历历在目。利用商业模式画布分析瑞士银行、Google、Lego、Wii 、Apple等跨国企业,归纳出三种不同的产业 模式,也涵括新近的热门现象免费效应及长尾理论等。在这些有趣的例子中,我们不仅更......一起来看看 《商业模式新生代》 这本书的介绍吧!