PHP json_last_error() 报错int(3)的解决办法

分类:PHP, 发布于 2020-03-07 23:56:41,评论(0)浏览(566)

在decode前执行:

$result =  preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', trim($result));


简述观察者模式

分类:PHP, 发布于 2018-04-27 05:37:15,评论(0)浏览(746)

观察者模式是对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。

image.png

DEMO:

/**
 * Class EventGenerator
 */
abstract class EventGenerator
{
    private $ObServers = [];

    //增加观察者
    public function add(ObServer $ObServer) {
        $this->ObServers[] = $ObServer;
    }

    //事件通知
    public function notify() {
        foreach ($this->ObServers as $ObServer) {
            $ObServer->update();
        }
    }
}

interface ObServer
{
    public function update($event_info = null);
}

class ObServer1 implements ObServer {
    public function update($event_info=null) {
        echo "ObServer1";
    }
}

class ObServer2 implements ObServer {
    public function update($event_info=null) {
        echo "ObServer2";
    }
}

class Event extends EventGenerator {
    public function trigger() {
        $this->notify();
    }
}

$event = new Event();
$event->add(new ObServer1());
$event->add(new ObServer2());
$event->trigger();


PHP性能分析工具xhprof安装及使用

分类:PHP, 发布于 2018-03-29 07:37:18,评论(0)浏览(745)

对于本地开发环境来说,进行性能分析xdebug是够用了,但如果是线上环境的话,xdebug消耗较大,配置也不够灵活,因此线上环境建议使用xhprof进行PHP性能追踪及分析。

1、安装xhprof

git clone https://github.com/longxinH/xhprof
cd xhprof/extension/
phpize
./configure
make && make install


然后在/etc/php.d/中加入xhprof.ini,内容为extension=xhprof.so

重启php-fpm

为了更直观,可以安装图片组件

yum install graphviz

至此xhprof扩展安装完成,下面是使用过程:

在文件入口输入启用方法

xhprof_enable();

在结束位置添加

$xhprof_data = xhprof_disable();


include_once $_SERVER['DOCUMENT_ROOT']."/xhprof_lib.php";
include_once $_SERVER['DOCUMENT_ROOT']."/xhprof_runs.php";

$xhprof_runs = new \XHProfRuns_Default();


$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

die("http://192.168.3.5/xhprof/xhprof_html/index.php?run=$run_id&source=xhprof_foo");


根据你的文件目录配置,点击以上地址查看分析结果

以下测试md5和sha1的结果:

image.png

1522308957892566.png

手动安装composer

分类:PHP, 发布于 2018-03-09 14:43:36,评论(0)浏览(729)

在国内大家懂的,安装composer往往需要手动安装。

将composer.phar下载,放入php.exe同目录,在该目录下建立composer.bat文件,内容写入@php "%~dp0composer.phar" %*即可,再次开起cmd窗口,输入composer就可以了,确保添加了环境变量。

lumen 按天创建日志文件

分类:PHP, 发布于 2017-03-06 10:40:51,评论(0)浏览(887)

在bootstrap/app.php文件中增加:

$app->configureMonologUsing(function(Monolog\Logger $monolog) use ($app) {
    return $monolog->pushHandler(
        new \Monolog\Handler\RotatingFileHandler($app->storagePath().'/logs/lumen.log')
    );
});


CentOS 7通过yum安装PHP7.1

分类:PHP, 发布于 2017-01-22 09:07:55,评论(0)浏览(1514)

CentOS 7/6下安装PHP7.1的步骤:

CentOS 7:

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

CentOS 6:

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm

安装PHP及相关包:

yum install mod_php71w php71w-opcache php71w-mysql php71w-gd php71w-mbstring

更多包在 https://webtatic.com/packages/php71/