您的位置 首页 php

后端开发干货:PHP源码阅读技巧

作者:张勇,腾讯IEG后台开发工程师

写在前面

时光匆匆 光阴似箭,今年已经是小张做码农的第N个年头了,得益于世界上最好的语言(PHP),小张的头发依旧茂密。但是项目写了一个又一个,PHP、swoole用到飞起,这些年的成长却大不如从前。

你是否也纠结于此,今天让我们换个角度看PHP,打开源码从C的角度看PHP,看一眼这个具有20年历史的庞大C项目是如何工作的。

从这一刻开始,你将荣升为C程序员,你可以去温习一下C的语法、结构体、指针和引用。

你也许会问,看个代码git拉一下用sublime打开不就好了吗,为什么要写个KM。确实这样可以看代码,但那叫看代码片段,不是看工程。PHP项目需要兼容Windows、Linux、Mac平台还要处理 线程安全 ,其代码包含大量宏定义。我们需要借助 IDE 的自动跳转和提示才能更好的阅读。

我们需要一个IDE无错误提示的项目。(当你要看一个方法却无法跳转的时候,真是太难受了)

后端开发干货:PHP源码阅读技巧

devCloud配置

devCloud是内部用于开发调试的linux虚拟机。没有的小伙伴可以自己准备一台可以ssh的CentOS虚拟机,本地虚拟机或云平台都可以。(最好网络能好一点,后面要传不少东西)

cmake re2c

后面我们需要使用Clion作为C项目的IDE,Clion需要使用cmake作为项目自动化构建工具。所以我们首先需要安装cmake devCloud上的 yum 默认的cmake版本为2.8.12.2符合要求,这里就直接用yum安装。

 yum install cmake
  

re2c 是语法生成器

 yum install re2c
  

gdb

devCloud的gdb版本过低,这里需要手动安装一个符合要求的版本,我选了7.8.2

 wget 
tar zxvf gdb-7.8.2.tar.gz
cd gdb-7.8.2
./configure
make
make install
  

代码拉取

选择一个php版本,这里我用7.1.33版本。 有几个理由:

1.项目中使用了这个版本。

2.php7对内核做了大量的改动,之前的php5已经不适合学习了。

3.后续会介绍的书籍也是基于php7.0的版本和php7.1比较接近。

4.php8目前还在测试,且和php7比较接近。

注意:本地远程都拉取一份,要保持 tag 一致,两边都要执行下面的cmake改造

  git  clone 
cd php-src
git tag -l
git checkout php-7.1.33
  

Clion配置

工欲善其事必先利其器,要看代码需要选一个好的IDE,多年使用PhpStorm的我推荐使用全家桶里面的Clion。一般我们不会直接使用Linux做桌面系统,这里使用Clion的远程部署功能配合devCloud做环境部署。我的本地是Windows环境,构建编译在远端的devCloud进行。(用 WSL 也是一样的,但是PHP的git仓库太大在WSL中做git操作非常卡,应该是WSL文件系统的bug)

PHP cmake改造

这一步是阻止大部分同学使用Clion查看PHP源码的主要原因。PHP项目由于历史悠久一直使用autoconfig作为项目自动管理工具。然而Clion并不支持autoconfig,它需要项目使用cmake构建,因此我们需要对php的源码做cmake构建改造。

官网有一份cmake的改造文档。

github上也有对应的代码。

但是这个文档和代码都太老了,应该是PHP5年代的代码。我对这个项目做了一些升级,兼容了PHP7(水平有限可能存在问题,欢迎提PR)。

在本地执行如下命令,clone php-cmake项目,切换分支,拷贝php-cmake/cmake中的所有文件到php源代码目录。注意目录合并。

 git clone 
cd php-cmake
git checkcout feature/support_php_7_1
cp -r cmake/* ../php-src //这里是php源码的路径
  

Toolchains配置

这里设置远程机器的信息,设置相关工具的路径。一般会自己识别,如果不能识别可以手动输入。会检查版本,默认devCloud上的版本好像都不正确需要自己升级。

后端开发干货:PHP源码阅读技巧

Deployment配置

这里需要设置代码同步的目录,设置好以后点按√好设置为默认。完成上面Toolchains设置后,这里可能默认出现一个devCloud的信息,但是Mappings 里面的Deployment path是/tmp/目录下面,你可以删除自己配置,也可以直接修改Deployment path到你需要的项目目录。

后端开发干货:PHP源码阅读技巧

注意:这里的Local path如果是windows上需要使用/分割路径,不是windows默认的,否则在执行cmake构建的时候会提示目录无法找到。

后端开发干货:PHP源码阅读技巧

在Options里面可以设置 不要同步.phpt文件,这是php项目中的 测试用例 文件。(我们在拉代码的时候其实已经会手动保证本地和远程机器的文件一致,但是Clion还会自己同步一次,设置这个可以加速同步速度。)

后端开发干货:PHP源码阅读技巧

同步代码到devCloud。首次同步真的很慢,你需要耐心等待。

后端开发干货:PHP源码阅读技巧

执行cmake构建,看看报错不?

后端开发干货:PHP源码阅读技巧

由于PHP cmake构建会在远端生成一些文件,我们需要同步这些文件到本地,否则打开代码会因为这部分.c .h文件不存在导致代码没有提示,各种报错。

后端开发干货:PHP源码阅读技巧

选择比对方式 用Size adn Timestamp 会快一点,这里比较慢,耐心等待一下。

后端开发干货:PHP源码阅读技巧

分析完成以后,就可以看到远程比本地多的文件了,我们需要把他们同步到本地。最上面的.clion.source.upload.marker 不需要同步,可以点击红框中的箭头取消。 之后点击同步。

后端开发干货:PHP源码阅读技巧

打开php_cli.c看看源码,是不是干干净净一个红线都没有。

断点

光看肯定是不行的,最好能把代码跑起来 打个断点可以更深入的了解运行机制。你可以用gdb去断点。我这里既然用了clion就直接用clion的断点功能,用起来更直观。

1、准备一个yong.php 写一点代码用来做测试文件。

2、这里配置运行配置。

后端开发干货:PHP源码阅读技巧

3、设置 arguments 和 Working directory

后端开发干货:PHP源码阅读技巧

4、 把yong.php文件同步到远端。执行以下看看。

后端开发干货:PHP源码阅读技巧

5、打断点。真的很爽~~

后端开发干货:PHP源码阅读技巧

开始阅读

目录结构

PHP项目有很多目录,大概功能如下:

 ├── CMake //用于cmake构建的文件,我们做cmake 改造的时候拷贝进去的。
├── TSRM //线程安全相关,你如果刚开始看就先忽略 不要看了
├── Zend //Zend内核的代码
├── appveyor //一些脚本 忽略
├── build //autoconf 构建用脚本 忽略
├── cmake-build-debug //cmake构建后产物 忽略
├── ext //扩展目录,这块可以找感兴趣的扩展看一下
├── main //主要的php逻辑 
├── netware //用于兼容Netware操作系统的 直接忽略
├── pear //忽略
├── sapi //入口部分,fpm  cgi  都在这里,我们可以先看一下cgi目录
├── scripts //脚本忽略
├── tests //测试用例 忽略
├── travis //构建用 忽略
└── win32 //兼容windows系统 忽略
  

其实和源代码相关的 只有这几个目录, 在Clion中标记为Source和Header目录方便IDE构建索引。

 main
extstandard
sapicli
Zend
  
后端开发干货:PHP源码阅读技巧

从哪里开始

准备工作都做好了,还有借口不看代码吗?如果你还是不知道从哪里开始,那我推荐从main函数开始吧。 它在sapicliphp_cli.c的最后面。 附图一张可以和源代码结合起来看。 图片引自:

最后推荐一本书 秦朋 的 《 PHP7内核剖析

后端开发干货:PHP源码阅读技巧

文章来源:智云一二三科技

文章标题:后端开发干货:PHP源码阅读技巧

文章地址:https://www.zhihuclub.com/80462.shtml

关于作者: 智云科技

热门文章

评论已关闭

37条评论

  1. IT staffers are in demand at hospitals and health systems across the country, and healthcare tech executives often struggle to find people to hire for the roles Tadalafil and Cialis are the same substance

  2. Tadalafil for prevention of erectile dysfunction after radiotherapy for prostate cancer the Radiation Therapy Oncology Group 0831 randomized clinical trial

  3. In rare instances, men taking PDE5 inhibitors oral erectile dysfunction medicines, including Viagra reported a sudden decrease or loss of vision Cialis and Levitra are drugs of a similar action that is slightly less popular than Viagra but are also very effective

  4. Making genomic medicine evidence based and patient centered a structured review and landscape analysis of comparative effectiveness research We use restorative and cosmetic procedures to get your smile back on track

  5. We are especially indebted with my lab partners from Alessio s lab who contributed to the experiments for this study, particularly with Pamela Becherini now a postdoc in Michele Cea s lab at our University; with Chiara Zucal and Alessandro Provenzani, from the CIBIO Trento, Italy, for their help in identifying the transcriptional effects of combined ET and fasting in BC cells, as well as for their tests of oestrogen receptor activity; with Else Driehuis and Hans Clevers, from the Hubrecht Institute Utrecht, The Netherlands, for performing experiments with their HR BC organoids; with Dr

  6. new defensive coordinator mike smith who is used to patrolling the sidelines will be calling the defensive plays from upstairs

  7. Pearson chi squared test was performed to compare the difference in incidence of PAN after treatment for AA

  8. It s a very simple anabolic steroid, very basic, but like so many things in life sometimes basic can be tremendous Studies of women diagnosed with early breast cancer were eligible for inclusion

  9. Considering the above results, we concluded that the 89 gene signature was more correlated with survival than histological classification, a finding which is also supported by previous reports 6, 14 Azria D, Hennequin C, Giraud P

  10. Locker Evanston Northwestern Healthcare, Northwestern University Feinberg School of Medicine, Evanston, IL, USA; Dr J Mackey Cross Cancer Institute, Edmonton, Alberta, Canada; Professor R

  11. Moreover, the administration of an ineffective dose of TAM 2 mg kg had no effect on memory retrieval in OVX rats, while pre test intra CA1 microinjection of mecamylamine 0

  12. However, somatic mutation could increase susceptibility to other seizure stimuli Alterations in several cancer pathways, including small cell lung carcinoma, renal cell carcinoma, and pathways in cancer were observed

  13. This takes about 15 seconds Interestingly, TG2 is able to hydrolyze the peptide histamine conjugates when the concentrations of substrates are lowered, thereby releasing deamidated gluten peptides that are stimulatory to T cells

  14. Novel Selective Estrogen Receptor Modulators Induce the Production of Th2 Type Cytokines IL10 and IL1RA

  15. A sore that does not heal or that bleeds easily A change in the color of mouth tissues A lump, thickening or rough spot in the mouth Pain, tenderness, or numbness anywhere in the mouth or on the lips Does Royal jelly affect tumor cells

  16. Acetylsalicylic acid is a non prescription drug while other NSAIDs do need prescription in most of the countries in the world

  17. com 20 E2 AD 90 20Viagra 20Benu 20Lekarna 20 20Manfaat 20Viagra 20Gold 20Usa viagra benu lekarna Leaders of the Church have often been Narcissuses, gratified and sickeningly excited by their courtiers

  18. Grundt A, et al Antenatal Bartter syndrome is characterized by massive polyuria that manifests in utero with the development of polyhydramnios, which in turn results in premature birth in most patients 111

  19. more than 10 difference between the two kidneys and or decreased or absent uptake of tracer in the renal cortex, causing distortion or indentation of the normal renal outline 20 087 ClГЎsico x 200 mL 8

  20. Emodin is a compound present in the root and rhizome of the Rheum palatum plant This testing and treatment usually requires the help of a physician skilled and knowledgeable in nutritional and natural medicine, who can also help with monitoring thyroid function

  21. There was no statistically significant difference in the number of myocardial infarctions, severe angina, or acute ischemic cardiac events between the two groups 61 tamoxifen citrate, 59 placebo; RR 1 He was allegedly seeking funds for his 2007election campaign

网站地图