#14 GitHub 上最热门的 PHP 项目

2021-08-06

以 stars:>10000 为条件过滤,得到 51 个结果,进行简单的过滤,结果如下:

  1. 最火的框架:
  2. Laravel
    1. Maatwebsite/Laravel-Excel
    2. barryvdh/laravel-debugbar
    3. barryvdh/laravel-ide-helper
    4. 管理 z-song/laravel-admin
    5. 管理 the-control-group/voyager
  3. Symfony
  4. CodeIgniter
  5. Yii2
  6. Slim
  7. phalcon/cphalcon
    特色:C语言写的
  8. PHP 包管理工具: Composer
  9. HTTP 客户端: Guzzle
  10. PHP 设计模式 DesignPatternsPHP
  11. 日志库: Seldaek/monolog
  12. 单元测试: sebastianbergmann/phpunit
  13. SMTP 库: PHPMailer
  14. 产品
  15. 文件存储: NextCloud
  16. CMS, WordPress
    1. WP 主题: roots/sage
  17. CMS, getgrav/grav
  18. CMS, octobercms/october
  19. 论坛, flarum/flarum
  20. Personal CRM, Monica
  21. Musici Streaming Server: koel/koel
  22. 网站统计: matomo-org/matomo
  23. 时间处理: briannesbitt/Carbon
  24. PHP Parser: nikic/PHP-Parser
  25. Markdown: erusev/parsedown
  26. 错误处理: filp/whoops
  27. Status Page System: CachetHQ/Cachet
  28. 不再维护 phacility/phabricator
    Phabricator is a collection of web applications for software development.
  29. 图像处理: Intervention/image
  30. 存储封装层: thephpleague/flysystem
  31. 环境变量: vlucas/phpdotenv
    Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically.
  32. 不再维护 PHPOffice/PHPExcel
    继任者: PHPOffice/PhpSpreadsheet
  33. UUID: ramsey/uuid
  34. 字符串处理: doctrine/inflector
  35. 代码静态分析: phpstan/phpstan
  36. 代码格式化, FriendsOfPHP/PHP-CS-Fixer
  37. doctrine/lexer
  38. doctrine/annotations
  39. doctrine/instantiator
  40. egulias/EmailValidator
  41. JWT tymondesigns/jwt-auth
  42. blueimp/jQuery-File-Upload
  43. fzaninotto/Faker

#13 PHP 版本

2021-04-12
{
  "4.0": {
    "4.0.0": "2000-05-22",
    "4.0.1": "2000-06-28",
    "4.0.2": "2000-08-29",
    "4.0.3": "2000-10-11",
    "4.0.4": "2000-12-19",
    "4.0.5": "2001-04-30",
    "4.0.6": "2001-06-23"
  },
  "4.1": {
    "4.1.0": "2001-12-10",
    "4.1.1": "2001-12-26",
    "4.1.2": "2002-02-27"
  },
  "4.2": {
    "4.2.0": "2002-04-22",
    "4.2.1": "2002-05-13",
    "4.2.2": "2002-07-22",
    "4.2.3": "2002-09-06"
  },
  "4.3": {
    "4.3.0": "2002-12-27",
    "4.3.1": "2003-02-17",
    "4.3.2": "2003-05-29",
    "4.3.3": "2003-08-25",
    "4.3.4": "2003-11-03",
    "4.3.5": "2004-03-26",
    "4.3.6": "2004-04-15",
    "4.3.7": "2004-06-03",
    "4.3.8": "2004-07-13",
    "4.3.9": "2004-09-22",
    "4.3.10": "2004-12-14",
    "4.3.11": "2005-03-31"
  },
  "4.4": {
    "4.4.0": "2005-07-11",
    "4.4.1": "2005-10-31",
    "4.4.2": "2006-01-11",
    "4.4.3": "2006-08-03",
    "4.4.4": "2006-08-17",
    "4.4.5": "2007-02-14",
    "4.4.6": "2007-03-01",
    "4.4.7": "2007-05-03",
    "4.4.8": "2008-01-03",
    "4.4.9": "2008-08-07"
  },
  "5.0": {
    "5.0.0": "2004-07-13",
    "5.0.1": "2004-08-12",
    "5.0.2": "2004-09-23",
    "5.0.3": "2004-12-15",
    "5.0.4": "2005-03-31",
    "5.0.5": "2005-09-05"
  },
  "5.1": {
    "5.1.0": "2005-11-24",
    "5.1.1": "2005-11-28",
    "5.1.2": "2006-01-12",
    "5.1.3": "2006-05-02",
    "5.1.4": "2006-05-04",
    "5.1.5": "2006-08-17",
    "5.1.6": "2006-08-24"
  },
  "5.2": {
    "5.2.0": "2006-11-02",
    "5.2.1": "2007-02-08",
    "5.2.2": "2007-05-03",
    "5.2.3": "2007-05-31",
    "5.2.4": "2007-08-30",
    "5.2.5": "2007-11-08",
    "5.2.6": "2008-05-01",
    "5.2.7": "2008-12-04",
    "5.2.8": "2008-12-08",
    "5.2.9": "2009-02-26",
    "5.2.10": "2009-06-18",
    "5.2.11": "2009-09-16",
    "5.2.12": "2009-12-17",
    "5.2.13": "2010-02-25",
    "5.2.14": "2010-07-22",
    "5.2.15": "2010-12-08",
    "5.2.16": "2010-12-16",
    "5.2.17": "2011-01-06"
  },
  "5.3": {
    "5.3.0": "2009-06-30",
    "5.3.1": "2009-11-19",
    "5.3.2": "2010-03-04",
    "5.3.3": "2010-07-22",
    "5.3.4": "2010-12-09",
    "5.3.5": "2011-01-06",
    "5.3.6": "2011-03-17",
    "5.3.7": "2011-08-18",
    "5.3.8": "2011-08-23",
    "5.3.9": "2012-01-10",
    "5.3.10": "2012-02-02",
    "5.3.11": "2012-04-26",
    "5.3.12": "2012-05-03",
    "5.3.13": "2012-05-08",
    "5.3.14": "2012-06-06",
    "5.3.15": "2012-07-19",
    "5.3.16": "2012-08-16",
    "5.3.17": "2012-09-13",
    "5.3.18": "2012-10-18",
    "5.3.19": "2012-11-22",
    "5.3.20": "2012-12-20",
    "5.3.21": "2013-01-17",
    "5.3.22": "2013-02-21",
    "5.3.23": "2013-03-14",
    "5.3.24": "2013-04-11",
    "5.3.25": "2013-05-09",
    "5.3.26": "2013-06-06",
    "5.3.27": "2013-07-11",
    "5.3.28": "2013-12-12",
    "5.3.29": "2014-08-14"
  },
  "5.4": {
    "5.4.0": "2012-03-01",
    "5.4.1": "2012-04-26",
    "5.4.2": "2012-05-03",
    "5.4.3": "2012-05-08",
    "5.4.4": "2012-06-06",
    "5.4.5": "2012-07-19",
    "5.4.6": "2012-08-16",
    "5.4.7": "2012-09-13",
    "5.4.8": "2012-10-18",
    "5.4.9": "2012-11-22",
    "5.4.10": "2012-12-20",
    "5.4.11": "2013-01-17",
    "5.4.12": "2013-02-21",
    "5.4.13": "2013-03-14",
    "5.4.14": "2013-04-11",
    "5.4.15": "2013-05-09",
    "5.4.16": "2013-06-06",
    "5.4.17": "2013-07-04",
    "5.4.18": "2013-08-15",
    "5.4.19": "2013-08-22",
    "5.4.20": "2013-09-19",
    "5.4.21": "2013-10-17",
    "5.4.22": "2013-11-14",
    "5.4.23": "2013-12-12",
    "5.4.24": "2014-01-09",
    "5.4.25": "2014-02-06",
    "5.4.26": "2014-03-06",
    "5.4.27": "2014-04-03",
    "5.4.28": "2014-05-01",
    "5.4.29": "2014-05-29",
    "5.4.30": "2014-06-26",
    "5.4.31": "2014-07-24",
    "5.4.32": "2014-08-21",
    "5.4.33": "2014-09-18",
    "5.4.34": "2014-10-16",
    "5.4.35": "2014-11-13",
    "5.4.36": "2014-12-18",
    "5.4.37": "2015-01-22",
    "5.4.38": "2015-02-19",
    "5.4.39": "2015-03-19",
    "5.4.40": "2015-04-16",
    "5.4.41": "2015-05-14",
    "5.4.42": "2015-06-11",
    "5.4.43": "2015-07-09",
    "5.4.44": "2015-08-06",
    "5.4.45": "2015-09-03"
  },
  "5.5": {
    "5.5.0": "2013-06-20",
    "5.5.1": "2013-07-18",
    "5.5.2": "2013-08-15",
    "5.5.3": "2013-08-22",
    "5.5.4": "2013-09-19",
    "5.5.5": "2013-10-17",
    "5.5.6": "2013-11-14",
    "5.5.7": "2013-12-12",
    "5.5.8": "2014-01-09",
    "5.5.9": "2014-02-06",
    "5.5.10": "2014-03-06",
    "5.5.11": "2014-04-03",
    "5.5.12": "2014-05-01",
    "5.5.13": "2014-05-29",
    "5.5.14": "2014-06-26",
    "5.5.15": "2014-07-24",
    "5.5.16": "2014-08-21",
    "5.5.17": "2014-09-18",
    "5.5.18": "2014-10-16",
    "5.5.19": "2014-11-13",
    "5.5.20": "2014-12-18",
    "5.5.21": "2015-01-22",
    "5.5.22": "2015-02-19",
    "5.5.23": "2015-03-19",
    "5.5.24": "2015-04-16",
    "5.5.25": "2015-05-14",
    "5.5.26": "2015-06-11",
    "5.5.27": "2015-07-09",
    "5.5.28": "2015-08-06",
    "5.5.29": "2015-09-03",
    "5.5.30": "2015-10-01",
    "5.5.31": "2016-01-07",
    "5.5.32": "2016-02-04",
    "5.5.33": "2016-03-03",
    "5.5.34": "2016-03-31",
    "5.5.35": "2016-04-28",
    "5.5.36": "2016-05-26",
    "5.5.37": "2016-06-23",
    "5.5.38": "2016-07-21"
  },
  "5.6": {
    "5.6.0": "2014-08-28",
    "5.6.1": "2014-10-02",
    "5.6.2": "2014-10-16",
    "5.6.3": "2014-11-13",
    "5.6.4": "2014-12-18",
    "5.6.5": "2015-01-22",
    "5.6.6": "2015-02-19",
    "5.6.7": "2015-03-19",
    "5.6.8": "2015-04-16",
    "5.6.9": "2015-05-14",
    "5.6.10": "2015-06-11",
    "5.6.11": "2015-07-10",
    "5.6.12": "2015-08-06",
    "5.6.13": "2015-09-03",
    "5.6.14": "2015-10-01",
    "5.6.15": "2015-10-29",
    "5.6.16": "2015-11-26",
    "5.6.17": "2016-01-07",
    "5.6.18": "2016-02-04",
    "5.6.19": "2016-03-03",
    "5.6.20": "2016-03-31",
    "5.6.21": "2016-04-28",
    "5.6.22": "2016-05-26",
    "5.6.23": "2016-06-23",
    "5.6.24": "2016-07-21",
    "5.6.25": "2016-08-18",
    "5.6.26": "2016-09-15",
    "5.6.27": "2016-10-13",
    "5.6.28": "2016-11-10",
    "5.6.29": "2016-12-08",
    "5.6.30": "2017-01-19",
    "5.6.31": "2017-07-06",
    "5.6.32": "2017-10-26",
    "5.6.33": "2018-01-04",
    "5.6.34": "2018-03-01",
    "5.6.35": "2018-03-29",
    "5.6.36": "2018-04-26",
    "5.6.37": "2018-07-19",
    "5.6.38": "2018-09-13",
    "5.6.39": "2018-12-06",
    "5.6.40": "2019-01-10"
  },
  "7.0": {
    "7.0.0": "2015-12-03",
    "7.0.1": "2015-12-17",
    "7.0.2": "2016-01-07",
    "7.0.3": "2016-02-04",
    "7.0.4": "2016-03-03",
    "7.0.5": "2016-03-31",
    "7.0.6": "2016-04-28",
    "7.0.7": "2016-05-26",
    "7.0.8": "2016-06-23",
    "7.0.9": "2016-07-21",
    "7.0.10": "2016-08-18",
    "7.0.11": "2016-09-15",
    "7.0.12": "2016-10-13",
    "7.0.13": "2016-11-10",
    "7.0.14": "2016-12-08",
    "7.0.15": "2017-01-19",
    "7.0.16": "2017-02-16",
    "7.0.17": "2017-03-16",
    "7.0.18": "2017-04-13",
    "7.0.19": "2017-05-11",
    "7.0.20": "2017-06-08",
    "7.0.21": "2017-07-06",
    "7.0.22": "2017-08-03",
    "7.0.23": "2017-08-31",
    "7.0.24": "2017-09-28",
    "7.0.25": "2017-10-26",
    "7.0.26": "2017-11-23",
    "7.0.27": "2018-01-04",
    "7.0.28": "2018-03-01",
    "7.0.29": "2018-03-29",
    "7.0.30": "2018-04-26",
    "7.0.31": "2018-07-19",
    "7.0.32": "2018-09-13",
    "7.0.33": "2018-12-06"
  },
  "7.1": {
    "7.1.0": "2016-12-01",
    "7.1.1": "2017-01-19",
    "7.1.2": "2017-02-16",
    "7.1.3": "2017-03-16",
    "7.1.4": "2017-04-13",
    "7.1.5": "2017-05-11",
    "7.1.6": "2017-06-07",
    "7.1.7": "2017-07-06",
    "7.1.8": "2017-08-03",
    "7.1.9": "2017-08-31",
    "7.1.10": "2017-09-28",
    "7.1.11": "2017-10-26",
    "7.1.12": "2017-11-23",
    "7.1.13": "2018-01-04",
    "7.1.14": "2018-02-01",
    "7.1.15": "2018-03-01",
    "7.1.16": "2018-03-29",
    "7.1.17": "2018-04-26",
    "7.1.18": "2018-05-24",
    "7.1.19": "2018-06-22",
    "7.1.20": "2018-07-19",
    "7.1.21": "2018-08-16",
    "7.1.22": "2018-09-13",
    "7.1.23": "2018-10-11",
    "7.1.24": "2018-11-08",
    "7.1.25": "2018-12-06",
    "7.1.26": "2019-01-10",
    "7.1.27": "2019-03-07",
    "7.1.28": "2019-04-04",
    "7.1.29": "2019-05-02",
    "7.1.30": "2019-05-30",
    "7.1.31": "2019-08-01",
    "7.1.32": "2019-08-29",
    "7.1.33": "2019-10-24"
  },
  "7.2": {
    "7.2.0": "2017-11-30",
    "7.2.1": "2018-01-04",
    "7.2.2": "2018-02-01",
    "7.2.3": "2018-03-01",
    "7.2.4": "2018-03-29",
    "7.2.5": "2018-04-26",
    "7.2.6": "2018-05-24",
    "7.2.7": "2018-06-21",
    "7.2.8": "2018-07-19",
    "7.2.9": "2018-08-16",
    "7.2.10": "2018-09-13",
    "7.2.11": "2018-10-11",
    "7.2.12": "2018-11-08",
    "7.2.13": "2018-12-06",
    "7.2.14": "2019-01-10",
    "7.2.15": "2019-02-07",
    "7.2.16": "2019-03-07",
    "7.2.17": "2019-04-04",
    "7.2.18": "2019-05-02",
    "7.2.19": "2019-05-30",
    "7.2.20": "2019-07-04",
    "7.2.21": "2019-08-01",
    "7.2.22": "2019-08-29",
    "7.2.23": "2019-09-26",
    "7.2.24": "2019-10-24",
    "7.2.25": "2019-11-21",
    "7.2.26": "2019-12-18",
    "7.2.27": "2020-01-23",
    "7.2.28": "2020-02-20",
    "7.2.29": "2020-03-19",
    "7.2.30": "2020-04-16",
    "7.2.31": "2020-05-14",
    "7.2.32": "2020-07-09",
    "7.2.33": "2020-08-06",
    "7.2.34": "2020-10-01"
  },
  "7.3": {
    "7.3.0": "2018-12-06",
    "7.3.1": "2019-01-10",
    "7.3.2": "2019-02-07",
    "7.3.3": "2019-03-07",
    "7.3.4": "2019-04-04",
    "7.3.5": "2019-05-02",
    "7.3.6": "2019-05-30",
    "7.3.7": "2019-07-04",
    "7.3.8": "2019-08-01",
    "7.3.9": "2019-08-29",
    "7.3.10": "2019-09-26",
    "7.3.11": "2019-10-24",
    "7.3.12": "2019-11-21",
    "7.3.13": "2019-12-18",
    "7.3.14": "2020-01-23",
    "7.3.15": "2020-02-20",
    "7.3.16": "2020-03-19",
    "7.3.17": "2020-04-16",
    "7.3.18": "2020-05-14",
    "7.3.19": "2020-06-11",
    "7.3.20": "2020-07-09",
    "7.3.21": "2020-08-06",
    "7.3.22": "2020-09-03",
    "7.3.23": "2020-10-01",
    "7.3.24": "2020-10-29",
    "7.3.25": "2020-11-26",
    "7.3.26": "2021-01-07",
    "7.3.27": "2021-02-04",
    "7.3.28": "2021-04-29",
    "7.3.29": "2021-07-01",
    "7.3.30": "2021-08-26",
    "7.3.31": "2021-09-23"
  },
  "7.4": {
    "7.4.0": "2019-11-28",
    "7.4.1": "2019-12-18",
    "7.4.2": "2020-01-23",
    "7.4.3": "2020-02-20",
    "7.4.4": "2020-03-19",
    "7.4.5": "2020-04-16",
    "7.4.6": "2020-05-14",
    "7.4.7": "2020-06-11",
    "7.4.8": "2020-07-09",
    "7.4.9": "2020-08-06",
    "7.4.10": "2020-09-03",
    "7.4.11": "2020-10-01",
    "7.4.12": "2020-10-29",
    "7.4.13": "2020-11-26",
    "7.4.14": "2021-01-07",
    "7.4.15": "2021-02-04",
    "7.4.16": "2021-03-04",
    "7.4.18": "2021-04-29",
    "7.4.19": "2021-05-06",
    "7.4.20": "2021-06-03",
    "7.4.21": "2021-07-01",
    "7.4.22": "2021-07-29",
    "7.4.23": "2021-08-26",
    "7.4.24": "2021-09-23"
  },
  "8.0": {
    "8.0.0": "2020-11-26",
    "8.0.1": "2021-01-07",
    "8.0.2": "2021-02-04",
    "8.0.3": "2021-03-04",
    "8.0.5": "2021-04-29",
    "8.0.6": "2021-05-06",
    "8.0.7": "2021-06-03",
    "8.0.8": "2021-07-01",
    "8.0.9": "2021-07-29",
    "8.0.10": "2021-08-26",
    "8.0.11": "2021-09-23"
  }
}

#12 转载:五种常见的 PHP 设计模式

2020-08-06

注:这是我在很久以前收藏的一篇文章,来自 IBM Developer,整理资料的时候被翻出来,贴出来算了。
设计模式只是为 Java™ 架构师准备的 —— 至少您可能一直这样认为。实际上,设计模式对于每个人都非常有用。如果这些工具不是 “架构太空人” 的专利,那么它们又是什么?为什么说它们在 PHP 应用程序中非常有用?本文解释了这些问题。

#10 PHP 5.2 与 PHP 5.3+ 的几处差异

2018-11-22

修改一个我之前开发的一个 Discuz 插件对于 PHP 5.2 的支持(当时插件开发环境是 PHP 5.6.37),发现以下几处需要注意的地方:

#9 PHP 中 foreach 循环赋值给引用的一个特性(坑)

2016-10-17

复现方法

创建一个存文本文件,命名为:test.php,内容如下:

<?php

$menu = [
    1 => ['id' => 1, 'name' => 'a', 'child' => []],
    2 => ['id' => 2, 'name' => 'b', 'child' => []],
];

foreach ($menu as &$submenu) {
    $submenu['class'] = empty($submenu['child']) ? '' : 'hasChild';
}

print_r($menu);

print str_repeat('-', 80) . "\n";

foreach ($menu as $submenu) {
    print_r($submenu);
}
?>

命令行中运行:

php -f test.php

得到以下输出:

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => a
            [child] => Array
                (
                )

            [class] =>
        )

    [2] => Array
        (
            [id] => 2
            [name] => b
            [child] => Array
                (
                )

            [class] =>
        )

)
--------------------------------------------------------------------------------
Array
(
    [id] => 1
    [name] => a
    [child] => Array
        (
        )

    [class] =>
)
Array
(
    [id] => 1
    [name] => a
    [child] => Array
        (
        )

    [class] =>
)

可以发现,第二个 foreach 中的输出是错误的。

来自官网的说明

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)

// without an unset($value), $value is still a reference to the last item: $arr[3]

foreach ($arr as $key => $value) {
    // $arr[3] will be updated with each value from $arr...
    echo "{$key} => {$value} ";
    print_r($arr);
}
// ...until ultimately the second-to-last value is copied onto the last value

// output:
// 0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 2 )
// 1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 4 )
// 2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
// 3 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
?>

参考

#8 FHS (Filesystem Hierarchy Standard)

2013-09-13

文件系统层次化标准

Version Release date
1.0 1994-02-14
1.1 1994-10-09
1.2 1995-03-28
2.0 1997-10-26
2.1 2000-04-12
2.2 2001-05-23
2.3 2004-01-29
3.0 2015-05-18

基础结构

目录 CentOS 7 Ubuntu 含义
/     根目录
/sys/     sysfs 虚拟FS,内核信息
/proc/     proc 虚拟FS,进程信息
/tmp/     临时文件
/home/     用户主目录
/root/      
/boot/     启动相关:内核 + grub
/bin/ /usr/bin/ /usr/bin/ 系统程序
/sbin/ /usr/sbin/ /usr/sbin/ 系统程序(系统管理)
/lib/ /usr/lib/ /usr/lib/ 动态库
/lib64/ /usr/lib64/ /usr/lib64/ 动态库
/usr/      
/etc/     配置文件
/var/      
/opt/      
/data/      
/srv/      
/dev/     设备
/mnt/     挂载点
/media/     挂载点
/lost+found/ -   使用中的已删除文件
  /run/ /run/  
    /snap/  
    /cdrom/  

/etc/

/var/

目录 含义
/var/log/  
/var/cache/  
/var/mail/  
/var/run/  

PS:较新版本中,/var/run/ 变成了 /run/ 的软链接。

/usr/

UNIX Software Resource

目录 含义
/usr/bin/ 应用程序
/usr/sbin/ 应用程序(系统管理)
/usr/lib/  
/usr/lib64/  
/usr/local/  
/usr/include/  
/usr/share/  
/usr/src/  

PS:/,/usr/,/usr/local/ 三层具有类似的目录结构
PS:较新版本中 /bin/、/sbin/、/lib/、/lib64/ 和 /usr/ 目录下的同名目录合并了。

用户主目录

参考资料与拓展阅读

#7 PHP FPM

2013-02-21

FPM 就是 PHP FastCGI Process Manager 的简称。

历史

最传统的方式是通过 Apache mod_php 模块提供服务,将 PHP 解释器内嵌到 Apache 进程内部。

另一条路就是通过 CGI/FastCGI。演化路线:php cgi -> php fastcgi -> php fpm

CGI (公共网关接口) 是一个非常简单的协议,每次来一个请求,CGI 就启动一个 PHP 进程,请求处理完成之后就退出进程。
PS: PHP 内置 CGI 支持(php-cgi)。
PS:php-cgi 也可以常驻内存,作为 FastCGI Worker 运行。

CGI 需要反复启动退出进程(解析配置文件,配置环境变量,加载拓展...),这个性能开销就非常大了,所以 CGI 很快被 FastCGI 所替代。
FastCGI 则是通过 CGI 进程常驻的方式提供服务,Web 服务器通过 FastCGI 协议将请求发给主进程 (master), 主进程又将请求分发给子进程(worker)进行处理。
这样的话性能得到的很大的改进。而且可以和数据库之间保持连接,进一步节省时间提高效率。

之前是使用 Apache 的 FastCGI 拓展模块 mod_fastcgi、mod_fcgid 来弄,这个模块是起到 CGI 进程管理的作用。
有人开发了 Spawn-FCGI(来自 lighttpd 项目,通用 FastCGI 管理)、PHP-FPM (作为 PHP 的补丁开发) 来提供 CGI 进程管理,将管理权从 Apache 拿过来,其中 FPM 和 PHP 结合更好,性能更胜一筹,后来被并入 PHP (从 5.4 开始)。

https://php-fpm.org/
https://www.php.net/manual/en/install.fpm.php

Apache + mod_proxy_fcgi + php-fpm

http://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html

加载 mod_proxy_fcgi 模块

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

SetHandler

<FilesMatch "\.php$">
    # Note: The only part that varies is /path/to/app.sock
    SetHandler  "proxy:unix:/path/to/app.sock|fcgi://localhost/"
</FilesMatch>

<FilesMatch ...>
    SetHandler  "proxy:fcgi://localhost:9000"
</FilesMatch>

<Proxy "balancer://myappcluster/">
    BalancerMember "fcgi://localhost:4000"
    BalancerMember "fcgi://localhost:4001"
</Proxy>
<FilesMatch ...>
    SetHandler  "proxy:balancer://myappcluster/"
</FilesMatch>

ProxyPass

ProxyPass "/myapp/" "fcgi://localhost:4000/"
ProxyPass "/myapp/" "fcgi://localhost:4000/" enablereuse=on
ProxyPass "/myapp/" "balancer://myappcluster/"

ProxyPassMatch

ProxyPassMatch "^/myapp/.*\.php(/.*)?$" "fcgi://localhost:9000/var/www/" enablereuse=on
ProxyPassMatch "^/(.*\.php(/.*)?)$" "unix:/var/run/php5-fpm.sock|fcgi://localhost/var/www/"

Nginx + php-fpm

Nginx 中的 fastcgi_pass 可以指定 FastCGI 进程,比如:fastcgi_pass 192.168.64.234:9000。

#6 PHP 函数的引用返回

2011-12-07

遇到函数声明时前面加引用符号的情况。

调用函数时加引用符号可以理解,就是声明的接受变量是一个引用嘛。(实验证明,这是错的,说引用类型只能赋值给变量。What?不知所云,求指教。)

可是函数声明就没有那么好理解了。

php.net 中的例子:

<?php
class foo {
    public $value = 42;
    public function &getValue() {
        return $this->value;
    }
}
$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue; // prints the new value of $obj->value, i.e. 2.
?>

本例中 getValue 函数所返回的对象的属性将被赋值,而不是拷贝,就和没有用引用语法一样。

还是没太明白,试验先。

eg 1、普通的函数声明,然后引用接收。

function foo() {
    $var = 0;
    return $var;
}
$get = & foo();
var_dump($get);
$get = 5;
$get = & foo();
var_dump($get);

结果:

Strict standards: Only variables should be assigned by reference
int 0
Strict standards: Only variables should be assigned by reference
int 0

eg 2、将上面例子中的函数声明成了引用返回。

function & foo() {
    $var = 0;
    return $var;
}
$get = foo();
var_dump($get);
$get = 5;
$get = foo();
var_dump($get);

结果:

int 0
int 0

和没有把函数声明成引用一样的效果。

eg 3、将上面例子中的函数声明成了引用返回。

function & foo() {
    static $var = 0;
    return $var;
}
$get = & foo();
var_dump($get);
$get = 5;
$get = & foo();
var_dump($get);

结果:

int 0

int 5

eg 4、面向对象中引用返回的应用。

/**
* 试验辅类,用于试验主类属性 ref 的 `->` 操作。
*/
class Ex {
public $some;
    function __construct() {
        $this->some = 0;
    }
    public function set($v) {
        $this->some = $v;
    }
    public function get() {
        return $this->some;
    }
}
/**
* 试验主类
*/
class MyClass {
    public $ref;
    function __construct() {
        $this->ref = & foo();
    }
}
function & foo() {
    $var = new Ex;
    return $var;
}
$class = new MyClass();
$class->ref->set(5);
var_dump($class->ref->get());

结果:

int 5

总结

  1. 一般来说,函数返回就是返回一个值。
  2. 引用就是计算出变量的地址。
  3. 引用返回是把返回变量的引用返回来了。而且,返回引用的格式决定了两边都要用 & 声明。
    若只是在函数那边声明引用,那么返回结果将依然是赋值给接收变量。是不是就是返回一个变量拷贝呢?
    若只是在接收变量那边声明引用就会报错,因为函数(还是返回的值?)不能用 & 求地址。