显示页面过去修订反向链接回到顶部 本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。 ====== PHP ====== * Official site: [[http://php.net|php.net]] * [[http://laravel-china.github.io/php-the-right-way/|PHP 之道]] - 这个能了解 PHP 的一般法则 * [[http://www.walu.cc/phpbook/preface.md|PHP 扩展开发及内核应用]] - 这个能了解 PHP 原理 ===== Composer ===== * Official site:[[https://getcomposer.org/]] * composer 项目安装目录''verdor'' ===== Syntax ===== ==== 代码组织 ==== * **include**:[[http://php.net/manual/zh/function.include.php|include]];当一个文件被包含时,其中所包含的代码继承了 include 所在行的变量范围。从该处开始,调用文件在该行处可用的任何变量在被调用的文件中也都可用。不过所有在包含文件中定义的函数和类都具有全局作用域。 * **require** 和 include 几乎完全一样,除了处理失败的方式不同之外 ==== 面向对象(OO) ==== * [[http://php.net/language.oop5|类与对象]] * [[http://php.net/language.oop5.traits|traits]] * 构造与析构( __construct, __destruct) * 如果子类中定义了构造函数则不会隐式调用其父类的构造函数。 * 要执行父类的构造函数,需要在子类的构造函数中调用'' parent::__construct()''。 * 如果子类没有定义构造函数则会如同一个普通的类方法一样从父类继承(假如没有被定义为 private 的话)。 ==== 函数式 ==== * 匿名函数(Anonymous functions); 没有自动继承父作用域变量, 想要继承必须用 use 关键字, 默认传值, 传引用必须用符号 "&"<code php> <?php $msg1='Hello '; $msg2='world'; $example = function () use ($msg1, &$msg2) { echo $msg1.$msg2."\n"; }; $example();// "Hello world" $msg1='nohello'; $msg2='whatever'; $example();// "Hello whatever" </code> * [[http://php.net/class.closure|闭包类]] * [[http://php.net/language.types.callable|Callback 回调类型 callable]] * [[http://php.net/function.call-user-func-array|call_user_func_array 函数]], 用于参数个数不确定时. ==== Notes ==== * <wrap em>clearstatcache</wrap>: [[http://php.net/manual/zh/function.clearstatcache.php|clearstatcache]], 当使用 stat(),lstat() 或者其它文件操作相关函数时,PHP 将缓存这些函数的返回信息以提供更快的性能。可以用 clearstatcache() 函数来清除被 PHP 缓存的该文件信息. * ''php:<nowiki>//</nowiki>'' IO 流:[[http://php.net/manual/zh/wrappers.php.php|official manual]], ''<nowiki>php://stdin</nowiki>'', ''<nowiki>php://stderr</nowiki>'', ''<nowiki>php://stderr</nowiki>'', ''<nowiki>php://input</nowiki>'', ''<nowiki>php://output</nowiki>'', ''<nowiki>php://temp</nowiki>'' 等等. * ''foreach'' 循环时想修改内容时需要加引用 ''foreach($arr as &$value)'', 但是! 加了这个引用就要额外注意很多点, 详见[[http://php.net/manual/zh/control-structures.foreach.php|php.net]]. 比如数组本身必须有引用, 比如循环结束要 ''unset($value)''. 真别扭! ===== Others ===== * Ubuntu 16.04 的 php7 包,默认没有 mbstring dom 等模块支持,需要安装 ''php7.0-mbstring'', ''php7.0-xml'' 等包 * Ubuntu 14.04 安装 php 环境: <code bash> # 安装 php5-cli sudo apt-get install php5-cli # 安装 php5-fpm sudo apt-get install php5-fpm </code> * nginx 中关于 php 的适用 pathinfo 模式的配置:<code> location ~ \.php { fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini ## With php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } </code> ===== Tips ===== * 输出对象信息:<code php> error_log(print_r($php_object, true)); </code> public/it/php.txt 最后更改: 2019/03/26 17:38由 oakfire