两个完全没有关联的类:
闭包:
附加状态
每次解析,编译,执行
常用设置
引入1000个脚本 还用include?
在运行时自动查找并加载php类
http://bit.ly/jspolsky
普通的url就是php流封装协议标识符的伪装
php://stdin 只读php流,其中数据来自标准输入
php://stdout 写入当前输出缓冲区
php:temp 读写内存 没有内存写入文件
流上下文(流参数) stream_context_create创建
流过滤器 stream_filter_append($handler,'string.toupper')
自定义流过滤器
是个php类,扩展内置的php_user_filter类 必须实现filter(),onCreate(),onClose()必须使用`am_filter_register()`函数注册自定义的流过滤器
自定义示例:
共享服务器、虚拟私有服务器、专用服务器、平台即服务
php-fpm php fastCGI进程管理器,用于管理php进程池的软件,用于接收和处理来自web服务器的请求。php-fpm软件会创建一个主进程,控制何时以及如何把http请求转发给一个或多个子进程处理
php应用压力测试:Apache Bench & Seige
配置
缓冲输出(攒够了 再返回给web服务器)
测试
单元测试框架 phpUnit &phpSpec
运行测试 vendor/bin/phpunit -c phpunit.xml
代码覆盖度 vendor/bin/phpunit -c phpunit.xml --coverage-html coverge
虚拟化
vitualbox
没有提供用户友好的界面,无法轻易的启动,配置,停止和销毁虚拟机
vagrant
用户友好的接口补足了vb虚拟机
容器
在此基础上架设虚拟机,是预先配置好的虚拟机
homestead是建立在vagrant之上的抽象,也是个vagrant容器,预先配置好了完整的软件栈
trait能把模块化的实现方式注入多个无关的类中。而且trait还能促进代码重用。也可以让这两个类实现同一个接口,不过,得再两个类中重复实现相同的地理编码功能。
生成器:
省内存
生成一个范围内的数据
function makeRange($length){
for($i=0;$i<$length;$i++){
yield $i;
}
}
闭包:
附加状态
function enclosePerson($name){
return function($doCommand) use ($name){
return sprintf('$s,$s',$name,$doCommand)
}
}
每个闭包实例都可以使用$this关键字获取闭包内部的状态。闭包对象的内部状态没什么用,bindTo和一个魔术方法可以使用bindTo把Closure对象内部状态绑定到其他对象上。bindTO方法的第二个参数很重要,作用是指定绑定闭包的那个对象所属的类。因此闭包可以访问绑定闭包的对象中受保护和私有的成员变量。把路由回调绑定到app实例上,能在回调函数中处理app实例的状态
$app = new App();
$app -> addRoute('/users/josh',function(){
$this->responseContentType = 'application/json;charset=utf8';
$this->responseBody = '{"name":"Josh"}';
})
$app->dispatch('/uers/josh');
zend opcache
每次解析,编译,执行
常用设置
opcache.validate_timestamps = 1 // "0" in production
opcache.revalidate_freq = 0 //ou can enable automatic cache revalidation with these php.ini configuration settings:
opcache.memory_consumption = 64
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 4000
opcache.fast_shutdown = 1
自带的服务器:
php -S localhost:4000
自动加载器
引入1000个脚本 还用include?
在运行时自动查找并加载php类
根据命名空间加载类。。。 文件系统和命名空间对应
PDO
$sql = "select id from users where email= :email";
$statement = $pdo->prepare($sql);
$email = filter_input(INPUT_GET, 'email'); // 过滤GET输入
$statement->bindValue(':email', $email);// 绑定sql参数 第三个参数指定类型
$statement->execute();
// Iterate results
while (($result = $statement->fetch(PDO::FETCH_ASSOC)) !== false) {
echo $result['email'];
}
字符编码
字符编码是打包Unicode数据的方式,以便把数据存储在内存中,或者通过线缆在服务器和客户端之间传输unicode
和utf-8
http://bit.ly/ts-unicodehttp://bit.ly/jspolsky
流
流的作用是在出发地和目的地之间传输数据。普通的url就是php流封装协议标识符的伪装
php://stdin 只读php流,其中数据来自标准输入
php://stdout 写入当前输出缓冲区
php:temp 读写内存 没有内存写入文件
流上下文(流参数) stream_context_create创建
流过滤器 stream_filter_append($handler,'string.toupper')
自定义流过滤器
是个php类,扩展内置的php_user_filter类 必须实现filter(),onCreate(),onClose()必须使用`am_filter_register()`函数注册自定义的流过滤器
自定义示例:
class DirtyWordsFilter extends php_user_filter {
/**
* @param resource $in Incoming bucket brigade
* @param resource $out Outgoing bucket brigade
* @param int $consumed Number of bytes consumed
* @param bool $closing Last bucket brigade in stream?
*/
public function filter($in, $out, &$consumed, $closing){
$words = array('grime', 'dirt', 'grease');
$wordData = array();
foreach ($words as $word) {
$replacement = array_fill(0, mb_strlen($word), '*');
$wordData[$word] = implode('', $replacement);
}
$bad = array_keys($wordData);
$good = array_values($wordData);
// Iterate each bucket from incoming bucket brigade
while ($bucket = stream_bucket_make_writeable($in)) {
// Censor dirty words in bucket data
$bucket->data = str_replace($bad, $good, $bucket->data);
// Increment total data consumed
$consumed += $bucket->datalen;
// Send bucket to downstream brigade
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}
主机:
共享服务器、虚拟私有服务器、专用服务器、平台即服务
php-fpm php fastCGI进程管理器,用于管理php进程池的软件,用于接收和处理来自web服务器的请求。php-fpm软件会创建一个主进程,控制何时以及如何把http请求转发给一个或多个子进程处理
php应用压力测试:Apache Bench & Seige
配置
缓冲输出(攒够了 再返回给web服务器)
测试
单元测试框架 phpUnit &phpSpec
运行测试 vendor/bin/phpunit -c phpunit.xml
代码覆盖度 vendor/bin/phpunit -c phpunit.xml --coverage-html coverge
虚拟化
vitualbox
没有提供用户友好的界面,无法轻易的启动,配置,停止和销毁虚拟机
vagrant
用户友好的接口补足了vb虚拟机
容器
在此基础上架设虚拟机,是预先配置好的虚拟机
homestead是建立在vagrant之上的抽象,也是个vagrant容器,预先配置好了完整的软件栈