<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; PHP扩展</title>
	<atom:link href="https://www.phppan.com/tag/php%e6%89%a9%e5%b1%95/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>PHP源码阅读笔记三十五：PHP中的SESSION实现之多种存储方式</title>
		<link>https://www.phppan.com/2010/12/php-source-code-35-session-save-handlers/</link>
		<comments>https://www.phppan.com/2010/12/php-source-code-35-session-save-handlers/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 01:02:35 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>
		<category><![CDATA[PHP源码]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1153</guid>
		<description><![CDATA[PHP源码阅读笔记三十五：PHP中的SESSION实现之多种存储方式 源码版本：php5.3.1 环境:VS2 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP源码阅读笔记三十五：PHP中的SESSION实现之多种存储方式<br />
源码版本：php5.3.1<br />
环境:VS2008</p>
<p>在php.ini中，可以看到配置项session.save_handler = files<br />
默认情况下，php.ini 中设置的 SESSION 保存方式是 files（session.save_handler = files），即使用读写文件的方式保存 SESSION 数据，而 SESSION 文件保存的目录由 session.save_path 指定，文件名以 sess_ 为前缀，后跟 SESSION ID，如：sess_sf26st2tfamqbarvnkfo2aftf2。文件中的数据即是序列化之后的 SESSION 数据了。如果访问量大，可能产生的 SESSION 文件会比较多，这时可以设置分级目录进行 SESSION 文件的保存，效率会提高很多，设置方法为：session.save_path=&#8221;N;/save_path&#8221;，N 为分级的级数，save_path 为开始目录。当写入 SESSION 数据的时候，PHP 会获取到客户端的 SESSION_ID，然后根据这个 SESSION ID 到指定的 SESSION 文件保存目录中找到相应的 SESSION 文件，不存在则创建之，最后将数据序列化之后写入文件。读取 SESSION 数据是也是类似的操作流程，对读出来的数据需要进行解序列化，生成相应的 SESSION 变量。如果需要对session文件设置mode，设置方法为：session.save_path = &#8220;N;MODE;/path&#8221;</p>
<p>同样我们可以使用session_set_save_handler函数设置session的处理方法。<br />
bool session_set_save_handler ( callback $open , callback $close , callback $read , callback $write , callback $destroy , callback $gc )<br />
那么此时可能需要思考一些问题，对于save_handler的设置是如何实现的？在内部存在怎样的结构存储这些处理方法？不同的存储方式之间是如何替换的？如果以一个扩展的方法添加，则需要实现哪些方法？这些方法在哪里添加到系统中的？</p>
<p><strong>【结构】</strong><br />
/ext/session/php_session.h文件</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define PS_OPEN_ARGS void **mod_data, const char *save_path, const char *session_name TSRMLS_DC</span>
<span style="color: #339933;">#define PS_CLOSE_ARGS void **mod_data TSRMLS_DC</span>
<span style="color: #339933;">#define PS_READ_ARGS void **mod_data, const char *key, char **val, int *vallen TSRMLS_DC</span>
<span style="color: #339933;">#define PS_WRITE_ARGS void **mod_data, const char *key, const char *val, const int vallen TSRMLS_DC</span>
<span style="color: #339933;">#define PS_DESTROY_ARGS void **mod_data, const char *key TSRMLS_DC</span>
<span style="color: #339933;">#define PS_GC_ARGS void **mod_data, int maxlifetime, int *nrdels TSRMLS_DC</span>
<span style="color: #339933;">#define PS_CREATE_SID_ARGS void **mod_data, int *newlen TSRMLS_DC</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* default create id function */</span>
PHPAPI <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>php_session_create_id<span style="color: #009900;">&#40;</span>PS_CREATE_SID_ARGS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> ps_module_struct <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>s_name<span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// session存储方式的名字 如默认的files</span>
	<span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>s_open<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>PS_OPEN_ARGS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>s_close<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>PS_CLOSE_ARGS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>s_read<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>PS_READ_ARGS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>s_write<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>PS_WRITE_ARGS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>s_destroy<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>PS_DESTROY_ARGS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>s_gc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>PS_GC_ARGS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>s_create_sid<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>PS_CREATE_SID_ARGS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> ps_module<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>第43到52行 定义ps_module，用于存放整个php_session结构，通过方法名应该可以很容易的识别出其作用意图。<br />
<strong>【以扩展方式支持session】</strong><br />
如果一个扩展模块需要支持session，则在其PHP_MINIT_FUNCTION方法中调用php_session_register_module方法。<br />
如在/ext/sqlite/sqlite.c 1395行，其调用了php_session_register_module(ps_sqlite_ptr);，说明在此扩展模块初始化时已经将其作为一个session的存储方式加载到内存中了。并且在此扩展中有sess_sqlite.c文件专门处理关于session中的应该定义的各方法，其分别以宏PS_OPEN_FUNC、PS_CLOSE_FUNC、PS_READ_FUNC、PS_WRITE_FUNC、PS_DESTROY_FUNC、PS_GC_FUNC、PS_CREATE_SID_FUNC等给出。这些宏的定义如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define PS_OPEN_FUNC(x) 	int ps_open_##x(PS_OPEN_ARGS)</span>
<span style="color: #339933;">#define PS_CLOSE_FUNC(x) 	int ps_close_##x(PS_CLOSE_ARGS)</span>
<span style="color: #339933;">#define PS_READ_FUNC(x) 	int ps_read_##x(PS_READ_ARGS)</span>
<span style="color: #339933;">#define PS_WRITE_FUNC(x) 	int ps_write_##x(PS_WRITE_ARGS)</span>
<span style="color: #339933;">#define PS_DESTROY_FUNC(x) 	int ps_delete_##x(PS_DESTROY_ARGS)</span>
<span style="color: #339933;">#define PS_GC_FUNC(x) 		int ps_gc_##x(PS_GC_ARGS)</span>
<span style="color: #339933;">#define PS_CREATE_SID_FUNC(x)	char *ps_create_sid_##x(PS_CREATE_SID_ARGS)</span></pre></td></tr></table></div>

<p>php_session_register_module方法的实现在/ext/session/session.c 1021行开始。如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define MAX_MODULES 10</span>
<span style="color: #339933;">#define PREDEFINED_MODULES 2</span>
&nbsp;
<span style="color: #993333;">static</span> ps_module <span style="color: #339933;">*</span>ps_modules<span style="color: #009900;">&#91;</span>MAX_MODULES <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	ps_files_ptr<span style="color: #339933;">,</span>
	ps_user_ptr
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
PHPAPI <span style="color: #993333;">int</span> php_session_register_module<span style="color: #009900;">&#40;</span>ps_module <span style="color: #339933;">*</span>ptr<span style="color: #009900;">&#41;</span> <span style="color: #808080; font-style: italic;">/* {{{ */</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> ret <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> MAX_MODULES<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>ps_modules<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ps_modules<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> ptr<span style="color: #339933;">;</span>
			ret <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> ret<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* }}} */</span></pre></td></tr></table></div>

<p>从上面的代码我们可能看到：PHP中的存储方式只能有10种，如果需要更多的方式，则需要修改MAX_MODULES的值，重新编译PHP。<br />
存储方式的添加顺序与对应的扩展模块加载顺序有关。</p>
<p><strong>【关于session_set_save_handler】</strong><br />
此函数用于将session的存储方式设置为用户自定义的函数，此函数的各参数对应ps_module结构的各个部分，并且会是新session.save_handler配置为user<br />
在将用户定义的函数设置为session的存储方式时会提前判断这些用户函数是否可用。如果存在一个函数不可用，则警告并退出，表示设置失败。</p>
<p><strong>【关于各方法的调用】</strong><br />
对于各存储方式定义的函数以类似于PS(mod)->s_open的方式调用。<br />
对于用户自定义的方法，通过PS(mod_user_names)保存函数名，在mod_user.c中以类似于retval = ps_call_handler(PSF(open), 2, args TSRMLS_CC);的方式调用。其中#define PSF(a) PS(mod_user_names).name.ps_##a</p>
<p>到此处理我们可以回答之前的问题。<br />
1、save_handler的设置是如何实现的？<br />
<strong>在php.ini中设置session.save_handler，在请求初始化时（PHP_RINIT_FUNCTION）查找对应的ps_module，将其赋值到全局变量PS(mod)，后续都是通过PS(mod)来处理session的存储操作，如果用户使用了session_set_save_handler方法设置存储方式，则此时的存储方式为user,通过user的模块方法中转调用用户指定的方法</strong><br />
2、在内部存在怎样的结构存储这些处理方法？<br />
<strong>此问题可以参见上面关于结构的说明，ps_module </strong><br />
3、不同的存储方式之间是如何替换的？<br />
<strong>可以在php.ini中设置session.save_handler修改，或者使用session_set_save_handler使用用户自定义的函数</strong><br />
4、如果以一个扩展的方法添加，则需要实现哪些方法？<br />
<strong>需要实现PS_OPEN_FUNC、PS_CLOSE_FUNC、PS_READ_FUNC、PS_WRITE_FUNC、PS_DESTROY_FUNC、PS_GC_FUNC、PS_CREATE_SID_FUNC等宏，这些分别对应ps_module的各个方法</strong><br />
5、这些方法在哪里添加到系统中的？<br />
<strong>通过php_session_register_module函数在模块初始化时加载</strong></p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/12/php-source-code-35-session-save-handlers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP扩展开发：简单类实现</title>
		<link>https://www.phppan.com/2010/11/php-extension-1-simple-class/</link>
		<comments>https://www.phppan.com/2010/11/php-extension-1-simple-class/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 01:52:16 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>
		<category><![CDATA[PHP扩展开发]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1079</guid>
		<description><![CDATA[PHP扩展开发：简单类实现 话说之前有写过如何在vs2008下开发PHP扩展，今天我们来实现一个简单类。 对于 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP扩展开发：简单类实现<br />
话说之前有写过如何在vs2008下开发PHP扩展，今天我们来实现一个简单类。<br />
对于一个类，一般包括类定义，声明成员变量，声明成员函数，定义类常量，<br />
对于类继承，重载，接口实现等不在这里说明。<br />
首先我们看下这个简单类的PHP实现：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * 简单类示例的PHP实现  2010-11-03 sz
 * @author phppan.p#gmail.com  http://www.phppan.com                               
 * 哥学社成员（http://www.blog-brother.com/）
 * @package test
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Phppan <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_name</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">CONST</span> URL <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.phppan.com'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_name <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>我们同样使用在之前创建的martin扩展<br />
下面说明其实现过程：<br />
1、声明类的成员函数<br />
在php_martin.h文件中声明类的成员函数，如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">&nbsp;
PHP_METHOD<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> __construct<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                                      
PHP_METHOD<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> __destruct<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
PHP_METHOD<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> getName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
PHP_METHOD<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> setName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>2、定义类的成员函数<br />
在martin.c文件中给出这4个函数的实现</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/** {{{ 
*/</span>
PHP_METHOD<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> __construct<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* }}} */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/** {{{ 
*/</span>
PHP_METHOD<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> __destruct<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* }}} */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/** {{{ 
*/</span>
PHP_METHOD<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> getName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	zval <span style="color: #339933;">*</span>self<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>name<span style="color: #339933;">;</span>
&nbsp;
	self <span style="color: #339933;">=</span> getThis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	name <span style="color: #339933;">=</span> zend_read_property<span style="color: #009900;">&#40;</span>Z_OBJCE_P<span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> self<span style="color: #339933;">,</span> ZEND_STRL<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;_name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span> TSRMLS_CC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	RETURN_STRING<span style="color: #009900;">&#40;</span>Z_STRVAL_P<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* }}} */</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/** {{{ 
*/</span>
PHP_METHOD<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> setName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>arg <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> arg_len<span style="color: #339933;">;</span>
	zval <span style="color: #339933;">*</span>value<span style="color: #339933;">,</span> <span style="color: #339933;">*</span>self<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>zend_parse_parameters<span style="color: #009900;">&#40;</span>ZEND_NUM_ARGS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> TSRMLS_CC<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>arg<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>arg_len<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> FAILURE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		WRONG_PARAM_COUNT<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	self <span style="color: #339933;">=</span> getThis<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	MAKE_STD_ZVAL<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ZVAL_STRING<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> arg<span style="color: #339933;">,</span> arg_len<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	SEPARATE_ZVAL_TO_MAKE_IS_REF<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	zend_update_property<span style="color: #009900;">&#40;</span>Z_OBJCE_P<span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> self<span style="color: #339933;">,</span> ZEND_STRL<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;_name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> value TSRMLS_CC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	RETURN_TRUE<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* }}} */</span></pre></td></tr></table></div>

<p>3、注册这些函数</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">&nbsp;
<span style="color: #808080; font-style: italic;">/* {{{ martin_functions[]
 *
 * Every user visible function must have an entry in martin_functions[].
 */</span>
<span style="color: #993333;">const</span> zend_function_entry martin_functions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	PHP_FE<span style="color: #009900;">&#40;</span>martin_echo<span style="color: #339933;">,</span>	NULL<span style="color: #009900;">&#41;</span>		
	PHP_ME<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> __construct<span style="color: #339933;">,</span> 	NULL<span style="color: #339933;">,</span> ZEND_ACC_PUBLIC<span style="color: #339933;">|</span>ZEND_ACC_CTOR<span style="color: #009900;">&#41;</span> 
	PHP_ME<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> __destruct<span style="color: #339933;">,</span>  	NULL<span style="color: #339933;">,</span> ZEND_ACC_PUBLIC<span style="color: #339933;">|</span>ZEND_ACC_DTOR<span style="color: #009900;">&#41;</span> 
	PHP_ME<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> getName<span style="color: #339933;">,</span> 	 	 	NULL<span style="color: #339933;">,</span> ZEND_ACC_PUBLIC<span style="color: #009900;">&#41;</span> 
	PHP_ME<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> setName<span style="color: #339933;">,</span> 	 	 	setName_args<span style="color: #339933;">,</span> ZEND_ACC_PUBLIC<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>NULL<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#125;</span>	<span style="color: #808080; font-style: italic;">/* Must be the last line in martin_functions[] */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* }}} */</span></pre></td></tr></table></div>

<p>4、在扩展模块初始化时初始化类，并声明成员变量和常量</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">zend_class_entry <span style="color: #339933;">*</span>phppan_ce<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 类方法的参数 */</span>
ZEND_BEGIN_ARG_INFO<span style="color: #009900;">&#40;</span>setName_args<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
	ZEND_ARG_INFO<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> name<span style="color: #009900;">&#41;</span>
ZEND_END_ARG_INFO<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* {{{ PHP_MINIT_FUNCTION
 */</span>
PHP_MINIT_FUNCTION<span style="color: #009900;">&#40;</span>martin<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/* If you have INI entries, uncomment these lines 
	REGISTER_INI_ENTRIES();
	*/</span>
	zend_class_entry phppan<span style="color: #339933;">;</span>
&nbsp;
	INIT_CLASS_ENTRY<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Phppan&quot;</span><span style="color: #339933;">,</span> martin_functions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	phppan_ce <span style="color: #339933;">=</span> zend_register_internal_class_ex<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>phppan<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> NULL TSRMLS_CC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #808080; font-style: italic;">/* 声明常量URL */</span>
	zend_declare_class_constant_stringl<span style="color: #009900;">&#40;</span>phppan_ce<span style="color: #339933;">,</span> ZEND_STRL<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;URL&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ZEND_STRL<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;http://www.phppan.com&quot;</span><span style="color: #009900;">&#41;</span> TSRMLS_CC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/* 声明私有成员变量 _name  */</span>
	zend_declare_property_null<span style="color: #009900;">&#40;</span>phppan_ce<span style="color: #339933;">,</span> ZEND_STRL<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;_name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> ZEND_ACC_PRIVATE TSRMLS_CC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #b1b100;">return</span> SUCCESS<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/* }}} */</span></pre></td></tr></table></div>

<p>以上为所有代码<br />
在这些代码里面有一些东西需要说明一下：<br />
1、类方法的定义与php的单个函数的定义一样，使用zend_function_entry结构数组，不同的是单个方法使用PHP_FE宏，<br />
类方法的定义使用PHP_ME宏，在h文件中使用ZEND_METHOD声明，普通的函数使用ZEND_FUNCTION声明。<br />
PHP_ME宏<br />
定义在php.h中<br />
#define PHP_ME          ZEND_ME<br />
#define ZEND_ME(classname, name, arg_info, flags)        ZEND_FENTRY(name, ZEND_MN(classname##_##name), arg_info, flags)<br />
2、在注册类时把该结构体作为参数交给相关的类注册方法即可<br />
ZEND_BEGIN_ARG_INFO(setName_args, 0)<br />
	ZEND_ARG_INFO(0, name)<br />
ZEND_END_ARG_INFO()<br />
3、取this变量使用getThis();<br />
4、使用zend_read_property读取类成员变量，返回的是zval 指针类型<br />
5、使用zend_update_property更新类成员变量。<br />
6、初始化类使用INIT_CLASS_ENTRY宏<br />
7、注册类使用zend_register_internal_class_ex函数</p>
<p>function registration failed duplicate name.问题的解决方法：<br />
这个由于我们在写这个简单类时偷了一下懒，将martin_functions作为模块的函数也将它作为了类的方法。<br />
解决方法是替换上面的martin_functions数组，增加phppan_functions，并且在注册时使用phppan_functions，在模块的functions字段使用martin_functions</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* {{{ martin_functions[]
 *
 * Every user visible function must have an entry in martin_functions[].
 */</span>
<span style="color: #993333;">const</span> zend_function_entry martin_functions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#123;</span>NULL<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#125;</span>  <span style="color: #808080; font-style: italic;">/* Must be the last line in martin_functions[] */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">const</span> zend_function_entry phppan_functions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    PHP_ME<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> __construct<span style="color: #339933;">,</span>     NULL<span style="color: #339933;">,</span> ZEND_ACC_PUBLIC<span style="color: #339933;">|</span>ZEND_ACC_CTOR<span style="color: #009900;">&#41;</span>
    PHP_ME<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> __destruct<span style="color: #339933;">,</span>      NULL<span style="color: #339933;">,</span> ZEND_ACC_PUBLIC<span style="color: #339933;">|</span>ZEND_ACC_DTOR<span style="color: #009900;">&#41;</span>
    PHP_ME<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> getName<span style="color: #339933;">,</span>             NULL<span style="color: #339933;">,</span> ZEND_ACC_PUBLIC<span style="color: #009900;">&#41;</span>
    PHP_ME<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> setName<span style="color: #339933;">,</span>             setName_args<span style="color: #339933;">,</span> ZEND_ACC_PUBLIC<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>NULL<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#125;</span>  <span style="color: #808080; font-style: italic;">/* Must be the last line in martin_functions[] */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* }}} */</span>
&nbsp;
INIT_CLASS_ENTRY<span style="color: #009900;">&#40;</span>phppan<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Phppan&quot;</span><span style="color: #339933;">,</span> phppan_functions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/11/php-extension-1-simple-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WIN7下VS2008下编译PHP扩展的6个细节</title>
		<link>https://www.phppan.com/2010/10/win7-vs2008-php-extension-2/</link>
		<comments>https://www.phppan.com/2010/10/win7-vs2008-php-extension-2/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 15:09:27 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1058</guid>
		<description><![CDATA[1、编译生成的dll文件无法加载的问题 此时apache启动时可能会报如下错误： PHP Warning: P [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><strong>1、编译生成的dll文件无法加载的问题</strong><br />
此时apache启动时可能会报如下错误：<br />
PHP Warning:  PHP Startup: Invalid library (maybe not a PHP library) &#8216;php_martin.dll&#8217; in Unknown on line 0<br />
原因：get_module在动态链接库中不对外开放<br />
修改：在vs2008的项目属性，选择【Configuration Properties】-> 【C/C++】-> 【Preprocessor】-> 【Preprocessor Definitions】增加（COMPILE_DL_MARTIN）宏定义<br />
查看方式：进入vs2008的命令行模式，进入dll所在文件夹，输入命令：dumpbin /exports php_martin.dll<br />
查看是否提供了get_module函数<br />
以上的martin需换成你自己的扩展名</p>
<p><strong>2、LNK2001: unresolved external symbol _ZVAL_ADDREF问题</strong><br />
在之前的文章<a href=”http://www.phppan.com/2010/10/php5-3-extension-lnk2001/”>PHP5.3版本编译扩展时出现:LNK2001: unresolved external symbol _ZVAL_ADDREF</a><br />
有提到解决方案，只是这样是将新的接口转换成旧的接口，这对于无法修改的旧代码可以适用，但是对于新的代码，我们建议在旧版本的时候使用Z_ADDREF_P将ZVAL_ADDREF替换，如下所示代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef Z_ADDREF_P</span>
<span style="color: #339933;">#define Z_ADDREF_P(x) ZVAL_ADDREF(x)</span>
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>

<p><strong>感谢<a href="http://www.laruence.com/">鸟哥</a>的指导</strong></p>
<p><strong>3、对于在被其它c文件include的c文件，在进行编译操作时需要将其从项目中排除掉。</strong></p>
<p><strong>4、Runtime Library</strong><br />
在编译时如遇到显示如下错误时：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">Error	<span style="color: #0000dd;">229</span>	error LNK2019<span style="color: #339933;">:</span> unresolved external symbol __imp___free_dbg referenced in <span style="color: #000000; font-weight: bold;">function</span>
&nbsp;
Error	<span style="color: #0000dd;">230</span>	error LNK2019<span style="color: #339933;">:</span> unresolved external symbol __imp___malloc_dbg referenced in <span style="color: #000000; font-weight: bold;">function</span> 
&nbsp;
Error	<span style="color: #0000dd;">231</span>	error LNK2019<span style="color: #339933;">:</span> unresolved external symbol __imp___strdup_dbg referenced in <span style="color: #000000; font-weight: bold;">function</span></pre></td></tr></table></div>

<p> LNK2019: unresolved external symbol __imp___free_dbg referenced<br />
在vs2008的项目属性，选择【Configuration Properties】-> 【C/C++】-> 【Code Generation】-> 【Runtime Library】,将其改为/MDd，而不是/MD<br />
<br /><strong>5、32位，64位问题</strong><br />
如有报错：Error	137	error C2466: cannot allocate an array of constant size 0<br />
这可能是VS2008 默认使用 64 位的 time_t 结构<br />
建议在命令中添加：/D &#8220;_USE_32BIT_TIME_T=1&#8243;<br />
<br /><strong>6、对于不同版本的dll编译，除了对应版本的源码外，所需要的php5ts.lib文件也要使用其相对应版本</strong><br />
否则会在链接时报LNK2019错误，如：<br />
error LNK2019: unresolved external symbol __imp__zend_str_tolower_dup referenced in function<br />
error LNK2019: unresolved external symbol __imp__gc_remove_zval_from_buffer referenced in function </p>
<p>&#8211;EOF&#8211;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/10/win7-vs2008-php-extension-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP5.3版本编译扩展时出现:LNK2001: unresolved external symbol _ZVAL_ADDREF</title>
		<link>https://www.phppan.com/2010/10/php5-3-extension-lnk2001/</link>
		<comments>https://www.phppan.com/2010/10/php5-3-extension-lnk2001/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 00:55:48 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>
		<category><![CDATA[PHP扩展编译]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1052</guid>
		<description><![CDATA[PHP5.3版本编译扩展时出现的问题 近，因要编译PHP扩展，本着最新最好的出发点，将PHP5.3的源码包下载 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP5.3版本编译扩展时出现的问题<br />
近，因要编译PHP扩展，本着最新最好的出发点，将PHP5.3的源码包下载下来，并将扩展源码创建VS2008项目，在经历百般磨难后，终于不再出现语法错误，然而当满怀希望按下F7键后，发现出现了N多的LNK2001和LNK2019错误，其中error LNK2001巨多，在纠结了一个周末后，可能是感动上天，终于在GOOGLE中找到了答案。<br />
报错如下：<br />
error LNK2001: unresolved external symbol _ZVAL_ADDREF<br />
GOOGLE给的答案是php5.3以上版本更改了一些Zend API，而ZVAL_ADDREF刚好是其中的一个。<br />
如下为解决方案：<br />
假设我们的扩展为martin,则在php_martin.h或martin文件中，在include了相关php本身的头文件后添加如下 代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">&nbsp;
<span style="color: #339933;">#if (PHP_MAJOR_VERSION == 5 &amp;&amp; PHP_MINOR_VERSION &gt;= 3) || (PHP_MAJOR_VERSION &gt;= 6)</span>
<span style="color: #339933;">#undef ZVAL_REFCOUNT</span>
<span style="color: #339933;">#undef ZVAL_ADDREF</span>
<span style="color: #339933;">#undef ZVAL_DELREF</span>
<span style="color: #339933;">#define ZVAL_REFCOUNT Z_REFCOUNT_P</span>
<span style="color: #339933;">#define ZVAL_ADDREF Z_ADDREF_P</span>
<span style="color: #339933;">#define ZVAL_DELREF Z_DELREF_P</span>
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>

<p><strong>以上代码的作用是将新的宏以旧名重定义，以保持其可用性。</strong></p>
<p>【参考资料】</p>
<p>http://www.hightman.cn/bbs/showthread.php?tid=656</p>
<p>http://d.hatena.ne.jp/rsky/20071016/1192524940</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/10/php5-3-extension-lnk2001/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>win7下用vs2008开发PHP扩展</title>
		<link>https://www.phppan.com/2010/10/win7-vs2008-php-extension/</link>
		<comments>https://www.phppan.com/2010/10/win7-vs2008-php-extension/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 12:49:48 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>
		<category><![CDATA[vs2008]]></category>
		<category><![CDATA[win7]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1033</guid>
		<description><![CDATA[win7下用vs2008开发PHP扩展 由于在win7下安装vc6比较麻烦，一直没有安装成功，只能改用vs20 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>win7下用vs2008开发PHP扩展<br />
由于在win7下安装vc6比较麻烦，一直没有安装成功，只能改用vs2008.<br />
由于要编译PHP扩展，于是做了一个简单PHP扩展开发的DEMO</p>
<p>环境：win7 + vs2008 + cygwin + php5.3.1 + xampp1.7.3<br />
生成过程如下：<br />
1、环境配置，安装cygwin,这个在生成PHP扩展的框架时有用到,我的安装目录为c:\cygwin<br />
2、安装xampp1.7.3，下载地址可以google下<br />
3、下载php5.3.1，下载地址：http://cn.php.net/get/php-5.3.1.tar.gz/from/cn2.php.net/mirror，解压到目录PHPHOME(我的是在D:\project\c\php-5.3.1)<br />
4、安装vs2008，这个时间会久一些<br />
5、如果以上的一切都安装好了，那么转第6步，我们开始开发扩展martin（这只是一个名称而已）<br />
<br />6、在命令行中，cd进入PHPHOME/ext/目录，输入php.exe ext_skel_win32.php &#8211;extname=martin，此时在ext目录下会生成martin文件夹及在此文件夹下与扩展相关的文件，包括php_martin.h,martin.c文件等。<br />
如果php.exe所在目录没有加到PATH中，请在php.exe前面加程序的完整路径<br />
<br />7、打开vs2008,新建基于已有文件的项目,选择VC++,选择文件所在目录，输入项目名称php_martin, 下一步，在Project type:中选择 DLL project,next直到完成。<br />
8、修改源码，<br />
   打开php_martin.h文件，找到PHP_FUNCTION(confirm_martin_compiled);在其下面增加一个扩展函数声明：PHP_FUNCTION(martin_echo);<br />
   打开martin.c文件，找到zend_function_entry martin_functions[],在其元素中添加 PHP_FE(martin_echo,	NULL)<br />
   在martin.c文件中添加如下代码</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">   PHP_FUNCTION<span style="color: #009900;">&#40;</span>martin_echo<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>arg <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	<span style="color: #993333;">int</span> arg_len<span style="color: #339933;">,</span> len<span style="color: #339933;">;</span>
	<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>strg<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>zend_parse_parameters<span style="color: #009900;">&#40;</span>ZEND_NUM_ARGS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> TSRMLS_CC<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>arg<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>arg_len<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> FAILURE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	len <span style="color: #339933;">=</span> spprintf<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>strg<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;This is %s's extension.the input string is %s.&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;martin&quot;</span><span style="color: #339933;">,</span> arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	RETURN_STRINGL<span style="color: #009900;">&#40;</span>strg<span style="color: #339933;">,</span> len<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>9、按F7 build此项目，此时会有文件找不到报错，此乃php部分源码没有包含的原因，右键项目属性，选择【Configuration Properties】-> 【C/C++】-> 【General】-> 【Additional Include Directories】，将源码根目录，main目录，TSRM目录，Zend目录添加到此处我的是（&#8221;D:\project\c\php-5.3.1&#8243;;&#8221;D:\project\c\php-5.3.1\main&#8221;;&#8221;D:\project\c\php-5.3.1\TSRM&#8221;;&#8221;D:\project\c\php-5.3.1\Zend&#8221;）<br />
10、继续build，此时可能会显示zend_config.h文件找不到，此时为部分宏没有定义,解决方案：选择项目属性，选择【Configuration Properties】-> 【C/C++】-> 【Preprocessor】-> 【Preprocessor Definitions】，在此处理添加 ZEND_DEBUG=0;COMPILE_DL_MARTIN;ZTS;ZEND_WIN32;PHP_WIN32;HAVE_MARTIN=1<br />
对于不同的扩展可以将COMPILE_DL_MARTIN和HAVE_MARTIN中的MARTIN替换成你的扩展名</p>
<p>11、继续build,此时可能会显示../main/config.w32.h没有找到，google了半天，所说在php5.2.9的源码中有此文件存在（此文件应该是某个环节生成的，没搞清楚），于是下载5.2.9，将此文件放到main目录下</p>
<p>12、继续build，此时可能会显示fatal error LNK1120，解决方案：打开项目属性，选择【Configuration Properties】-> 【Linker】->【Input】->【Additional Dependencies】，在此处添加php5ts.lib，另外需要在【Tools】->【Options】->【Projects and Solutions】->【VC++ Directories】，在【Show directories for:】下拉中，选择Librafy files，将php5ts.lib所在的路径添加进来，此文件存在于 二进制版本的dev/lib目录下。</p>
<p>13、右键solution属性，将Configuration选择为Release<br />
14、build，在ext\martin\Release下会有生成你一个以你的项目名为名称的dll文件(我的为php_martin.dll)<br />
15、将php_martin.dll文件拷贝到机器中运行的php所在的ext目录，修改php.ini文件，添加一行：extension=php_martin.dll，重启apache，<br />
16，运行一个包含了 echo martin_echo(&#8220;phppan.com&#8221;);语句的php文件，可以看到有输出This is martin&#8217;s extension.the input string is phppan.com.</p>
<p>如果你在启动apache中有报错为:<br />
PHP Warning:  PHP Startup: martin: Unable to initialize module\nModule compiled with build ID=API20090626,TS\nPHP    compiled with build ID=API20090626,TS,VC6\nThese options need to match\n in Unknown on line 0<br />
<br />
<b>Warning</b>:  PHP Startup: martin: Unable to initialize module<br />
Module compiled with build ID=API20090626,TS<br />
PHP    compiled with build ID=API20090626,TS,VC6<br />
These options need to match<br />
 in <b>Unknown</b> on line <b>0</b><br />
<strong>你需要在main/config.w32.h中添加如下语句<br />
#define PHP_COMPILER_ID &#8220;VC6&#8243;</strong><br />
此处来源于：<a href="http://hi.baidu.com/zhangsilly/blog/item/7a3c7f73f7dcad108701b082.html">PHP5.3系列设置编译器ID（build ID）</a></p>
<p>&#8211;EOF&#8211;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/10/win7-vs2008-php-extension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP中$_GET变量与$_REQUEST变量中取得的GET的值</title>
		<link>https://www.phppan.com/2009/12/php-get-request/</link>
		<comments>https://www.phppan.com/2009/12/php-get-request/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 00:30:21 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>
		<category><![CDATA[PHP源码]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=444</guid>
		<description><![CDATA[PHP中$_GET变量与$_REQUEST变量中取得的GET的值 曾经我以为$_REQUEST变量的值是$_G [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP中$_GET变量与$_REQUEST变量中取得的GET的值<br />
曾经我以为$_REQUEST变量的值是$_GET、$_POST、$_COOKIE三个变量的集合，所不同的是它会依据php.ini的配置进行合并</p>
<p>然而当有一天我以赋值的方式改变了$_GET的值，并从$_REQUEST变量中取时，我无法取到我所需要的值。<br />
于是，我做了如下DEMO:<br />
代码所示：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>访问localhost/test/index.php?tt=1<br />
输出如下：<br />
array &#8216;tt&#8217; => string &#8217;1&#8242; (length=1)<br />
array &#8216;tt&#8217; => string &#8217;1&#8242; (length=1) &#8216;test&#8217; => int 1<br />
array &#8216;tt&#8217; => string &#8217;1&#8242; (length=1)<br />
结果是$_REQUEST并没有$_GET的值。</p>
<p>思考。。。</p>
<p>【思考结果】<br />
$_REQUEST变量的值是$_GET、$_POST、$_COOKIE三个变量的集合这并没有错<br />
只是它是在PHP的生命周期开始时，这几个变量产生就产生了，分别作为一个全局变量存储在hash_table中<br />
当以赋值的方式改变$_GET的值时，并没有改变$_REQUEST的值。它们是独立存在的。</p>
<p>【扩展代码阅读】<br />
在研究过程中查看了PHP相关源码，$_REQUEST的生成过程如下：<br />
其过程如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>sapi<span style="color: #339933;">/</span>apache<span style="color: #339933;">/</span>mod_php5.<span style="color: #202020;">c</span> line <span style="color: #0000dd;">939</span><span style="color: #009900;">&#93;</span> hp_init_handler<span style="color: #009900;">&#40;</span>server_rec <span style="color: #339933;">*</span>s<span style="color: #339933;">,</span> pool <span style="color: #339933;">*</span>p<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">--&gt;</span><span style="color: #009900;">&#91;</span>sapi<span style="color: #339933;">/</span>apache<span style="color: #339933;">/</span>mode_php5.<span style="color: #202020;">c</span> line <span style="color: #0000dd;">290</span><span style="color: #009900;">&#93;</span> php_apache_startup<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>apache_sapi_module<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">--&gt;</span><span style="color: #009900;">&#91;</span>main<span style="color: #339933;">/</span>main.<span style="color: #202020;">c</span> line <span style="color: #0000dd;">1630</span><span style="color: #009900;">&#93;</span> php_module_startup<span style="color: #009900;">&#40;</span>sapi_module_struct <span style="color: #339933;">*</span>sf<span style="color: #339933;">,</span> zend_module_entry <span style="color: #339933;">*</span>additional_modules<span style="color: #339933;">,</span> uint num_additional_modules<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">--&gt;</span><span style="color: #009900;">&#91;</span>main<span style="color: #339933;">/</span>php_variables.<span style="color: #202020;">c</span> line <span style="color: #0000dd;">881</span><span style="color: #009900;">&#93;</span> php_startup_auto_globals<span style="color: #009900;">&#40;</span>TSRMLS_D<span style="color: #009900;">&#41;</span>
<span style="color: #339933;">--&gt;</span><span style="color: #009900;">&#91;</span>main<span style="color: #339933;">/</span>php_varables.<span style="color: #202020;">c</span> line <span style="color: #0000dd;">834</span><span style="color: #009900;">&#93;</span> php_auto_globals_create_request<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>name<span style="color: #339933;">,</span> uint name_len TSRMLS_DC<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>over.</p>
<p>变量的取值过程可以参考雪候鸟的“在PHP Module中获取$_GET/$_POST/$_COOKIE的方法研究”<br />
地址如下：<a href="http://www.laruence.com/2008/04/04/17.html">http://www.laruence.com/2008/04/04/17.html</a></p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/12/php-get-request/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP的生命周期</title>
		<link>https://www.phppan.com/2009/10/php-life-cycle/</link>
		<comments>https://www.phppan.com/2009/10/php-life-cycle/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 00:40:08 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=359</guid>
		<description><![CDATA[PHP的生命周期 php本身的生命周期是在命令行执行php test.php程序的生命周期（也就是cli） 整 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>PHP的生命周期</p>
<p>php本身的生命周期是在命令行执行php test.php程序的生命周期（也就是cli）</p>
<p>整个过程如下：</p>
<p>执行php test.php</p>
<p>调用每个扩展的模块初始化程序</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;请求test.php程序</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;调用每个扩展的请求初始化程序</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行test.php程序</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;调用每个扩展的请求关闭程序</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;释放内存等清除工作</p>
<p>调用每个扩展的模块关闭程序</p>
<p>终止php</p>
<p>如果PHP运行在WEB服务器中，那么它的生命周期就会有些不同了，这里又要根据服务器的不同分为以下三种：</p>
<p>1、单进程</p>
<p>模块初始化</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;请求初始化</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行脚本</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;关闭请求</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;请求初始化</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行脚本</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;关闭请求<br />
&nbsp;&nbsp;&nbsp;&nbsp;请求初始化<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行脚本<br />
&nbsp;&nbsp;&nbsp;&nbsp;关闭请求<br />
&nbsp;&nbsp;&nbsp;&nbsp;请求初始化<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行脚本<br />
&nbsp;&nbsp;&nbsp;&nbsp;关闭请求<br />
……</p>
<p>……<br />
……<br />
模块关闭</p>
<p>单进程的WEB服务器只对模块初始化一次，所有的页面请求都在其中</p>
<p>2、多进程</p>
<p>模块初始化                         模块初始化                    模块初始化                模块初始化</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;请求初始化                         请求初始化                    请求初始化                请求初始化</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行脚本                            执行脚本                      执行脚本                   执行脚本</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;关闭请求                            关闭请求                      关闭请求                   关闭请求</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;请求初始化                         请求初始化                    请求初始化                请求初始化</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行脚本                            执行脚本                      执行脚本                   执行脚本</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;关闭请求                            关闭请求                      关闭请求                   关闭请求</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;请求初始化                         请求初始化                    请求初始化                请求初始化</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;执行脚本                            执行脚本                      执行脚本                   执行脚本</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;关闭请求                            关闭请求                      关闭请求                   关闭请求</p>
<p>……                                  ……                            ……                         ……</p>
<p>关闭模块                            关闭模块                       关闭模块                    关闭模块</p>
<p>多进程只是把单进程复制了多份，各个子进程间无法共享数据等。</p>
<p>3、多线程</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;模块初始化</p>
<p>请求初始化                         请求初始化                    请求初始化                请求初始化</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;执行脚本                            执行脚本                      执行脚本                   执行脚本</p>
<p>关闭请求                            关闭请求                      关闭请求                   关闭请求</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关闭模块</p>
<p>全局变化可以在初始化的时候建立，并且只建立一次。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/10/php-life-cycle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP扩展之smart_str分析</title>
		<link>https://www.phppan.com/2009/10/php-smart_str/</link>
		<comments>https://www.phppan.com/2009/10/php-smart_str/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 08:27:09 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=354</guid>
		<description><![CDATA[php_smart_str_public.h 在php_smart_str_public.h文件中我们找到了如 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>php_smart_str_public.h<br />
在php_smart_str_public.h文件中我们找到了如下定义：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>c<span style="color: #339933;">;</span>
              <span style="color: #993333;">size_t</span> len<span style="color: #339933;">;</span>
              <span style="color: #993333;">size_t</span> a<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> smart_str<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>其中，size_t的定义 为：typedef unsigned int size_t;<br />
char *c 表示整个字符串<br />
size_t len表示整个字符串的实际长度<br />
size_t a 表示这个字符的总长度（smart_str的字符串总长度总是以128字节的倍数增加）</p>
<p>从php_smart_str.h文件所在的源码中我们可以看出它的空间分配具体有如下特点：<br />
1、在初始化时，如果所给字符串的长度小于SMART_STR_START_SIZE（定义为78字节），则初始smart_str的总长度为78字节（即a值），并给它分配空间；如果所给字符串的长度大于SMART_STR_START_SIZE，则将此长度上加上SMART_STR_PREALLOC（定义为128字节）作为smart_str的总长度，并为之分配空间。<br />
2、在已有字符串后添加时，如果添加后的总长度小于当前smart_str->a的值，则无需分配空间，否则，在smart_str新的长度的基础上再加上128字节的空间。<br />
这样有些类似于操作系统中内存以页为单位分配，它的好处是对齐内存地址，提高访问速度。<br />
在php_smart_str.h文件中，我们可以看到一些针对smart_str类型的宏定义的操作方法，方法列表如下：</p>
<p>smart_str_0 如果字符串存在，给字符串的最后添加’\0’;<br />
smart_str_alloc 在初始化和添加字符串时进行空间的分配；<br />
smart_str_appends_ex(dest, src, what) 在一个smart_str变量后面添加一个字符串，它分为两种空间分配方法，当what为真是，它使用__zend_realloc函数，此函数为永久分配内存，在无法分配内存时会报Out of memory的，当what为假时，它使用标准空间分配，即ZendMM所我有的分配函数erealloc(ptr, size)。<br />
smart_str_appends(dest, src) 在一个smart_str变量后面添加一个原生的字符串<br />
smart_str_appendc(dest, c)  在一个smart_str变量后面添加一个字符<br />
smart_str_free(s) 消除smart_str变量，释放所占内存<br />
smart_str_appendl(dest, src, len) 在一个smart_str变量后面添加一个长度为len的字符串<br />
smart_str_append(dest, src) 在一个smart_str变量后面添加一个smart_str字符串<br />
smart_str_append_long(dest, val) 将一个long的数字转化成字符串后添加到smart_str的后面smart_str_append_unsigned(dest, val) 将一个unsigned long的数字转化成字符串后添加到smart_str的后面<br />
smart_str_appendc_ex(dest, ch, what) 与smart_str_appends_ex类似，所不同的是它添加的是一个字符。<br />
smart_str_setl(dest, src, nlen) 将一个长为len的原生的字符串强制转化为一个smart_str<br />
smart_str_sets(dest, src) 将一个原生的字符串强制转化为一个smart_str</p>
<p>【经典代码】<br />
数字转字符串：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">char</span> __b<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">32</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>             
<span style="color: #993333;">int</span> __num <span style="color: #339933;">=</span> <span style="color: #0000dd;">28790</span><span style="color: #339933;">;</span>
<span style="color: #993333;">char</span> <span style="color: #339933;">*</span>__p<span style="color: #339933;">;</span>
__p <span style="color: #339933;">=</span> __b <span style="color: #339933;">+</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>__b<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #339933;">*</span>__p<span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span>                                                                                                                                                                                     
<span style="color: #b1b100;">do</span><span style="color: #009900;">&#123;</span>                                                                                                                                                                                        
 <span style="color: #339933;">*--</span>__p <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>__num <span style="color: #339933;">%</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #ff0000;">'0'</span><span style="color: #339933;">;</span>                                                                                                 
__num <span style="color: #339933;">/=</span><span style="color: #0000dd;">10</span><span style="color: #339933;">;</span>                                                                                                                                                                       
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>__num <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/10/php-smart_str/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSON格式总结</title>
		<link>https://www.phppan.com/2009/10/json-summary/</link>
		<comments>https://www.phppan.com/2009/10/json-summary/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 02:07:31 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[程序相关]]></category>
		<category><![CDATA[JAVASCRIPT]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP扩展]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=299</guid>
		<description><![CDATA[【JSON是什么】 JSON，JavaScript Object Notation，一种更轻、更友好的用于接口 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>【JSON是什么】<br />
JSON，JavaScript Object Notation，一种更轻、更友好的用于接口(AJAX、REST等)数据交换的格式。JSON是结构化数据串行化的文本格式，作为XML的一种替代品，用于表示客户端与服务器间数据交换有效负载的格式。它是从ECMAScript语言标准衍生而来的。JSON的设计目标是使它成为小的、轻便的、文本的，而且是JavaScript的一个子集。<br />
JSON能够描述四种简单的类型（字符串、数字、布尔值和null）和两种结构化类型（对象和数组）。</p>
<p>字符串(string)是零个或多个Unicode字符的序列。除了字符 &#8220;、\、/和一些控制符（\b，\f，\n，\r，\t）需要编码外，其他 Unicode 字符可以直接输出</p>
<p>对象(Object)是无次序的零个或多个名/值(name/value)对的集合，使用｛｝包含包含所有元素。这里的name是string类型，value则可以是string、number、boolean、null、Object或Array类型。</p>
<p>数组(Array)是零个或多个value的有序序列。JSON 还可以表示一个数组对象，使用 [] 包含所有元素，每个元素用逗号分隔，元素可以是任意的 Value。</p>
<p>Object 对象在 JSON 中是用 {} 包含一系列无序的 Key-Value 键值对表示的，key是string类型，value则可以是string、number、boolean、null、Object或Array类型。</p>
<p>&#8220;Object&#8221;和&#8221;Array&#8221;这两个术语来自JavaScript规范。</p>
<p>【JSON的优点】</p>
<ol>
<li> 数据格式比较简单, 易于读写, 格式都是压缩的, 占用带宽小</li>
<li> 易于解析, 客户端JavaScript可以简单的通过eval()进行JSON数据的读取</li>
<li> 支持多种语言, 包括ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby等语言服务器端语言, 便于服务器端的解析</li>
</ol>
<p>【JSON的缺点】</p>
<ol>
<li> 没有XML格式这么推广的深入人心和使用广泛, 没有XML那么通用性</li>
<li> JSON格式目前在Web Service中推广还属于初级阶段</li>
</ol>
<p>【在PHP中使用JSON】<br />
PHP中的json直接相关的函数只有json_encode和json_decode。其中json_encode只能接受 UTF-8 编码的字符串类型数据，所以此处我们可能用到iconv等编码转换函数。</p>
<p>在PHP５.2.0之后，可以使用json_encode直接操作服务器端的对象、数组等，能够直接生JSON格式, 便于客户端的访问提取。<br />
另外，由于PHP中的数组是以HASH链表存在，可以使用非数字的关键字作为下标，所以，如果我们需要生成的数据是数组而不是对象时，需要数据的下标满足如下要求：</p>
<ul>
<li> 必须是数字索引，</li>
<li> 必须从0开始，</li>
<li> 必须从小到依次增加，</li>
<li> 中间不可以弹跳下,</li>
<li> 位置不可变动.</li>
</ul>
<p>这是由于在JS中数组是0开始的顺序序列，其余都只能是哈希表对象。如果要使用数组，可以使用array_values()函数。</p>
<p>【小结】<br />
JSON 已经是 JavaScript. 标准的一部分。目前，主流的浏览器对 JSON 支持都非常完善。应用 JSON，我们可以从 XML 的解析中摆脱出来，对那些应用 Ajax 的 Web 2.0 网站来说，JSON 确实是目前最灵活的轻量级方案。</p>
<p>【参考资料】</p>
<p>http://ssgemail.javaeye.com/blog/36776</p>
<p>http://blog.csdn.net/kinglino520/archive/2009/03/30/4036449.aspx</p>
<p>http://hi.baidu.com/zhaofei299/blog/item/79ba4bf3473012c30b46e0d3.html</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/10/json-summary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用VC6.0生成VLD扩展</title>
		<link>https://www.phppan.com/2009/09/use-vc6-create-vld-extend/</link>
		<comments>https://www.phppan.com/2009/09/use-vc6-create-vld-extend/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 15:00:36 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP扩展]]></category>
		<category><![CDATA[VLD]]></category>
		<category><![CDATA[VLD扩展]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[使用VC6.0]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=212</guid>
		<description><![CDATA[使用VC6.0生成VLD扩展 最近想看看PHP生成的源码是啥样子，于是google半天，终于完成。 环境：VC [&#8230;]]]></description>
				<content:encoded><![CDATA[<div class="mceTemp mceIEcenter">使用VC6.0生成VLD扩展</div>
<p>最近想看看PHP生成的源码是啥样子，于是google半天，终于完成。<br />
环境：VC6.0<br />
源码：php5.2.9源码包（可以去http://www.php.net/下载），解压，我的是解压在D盘根目录下。<br />
Vld源码包（可以去http://pecl.php.net/package/vld/0.9.1下载），解压</p>
<p>生成过程如下：<br />
1、在Windows平台启动 VC++ 6.0, 【文件】（File）-&gt;【新建】（New）, 在 【工程】（Project）中选择 【Win32 Dynamic-Link Library】, 填写上 【工程名称】（Project Name） 和 【位置】（Location）,如图1所示：</p>
<div id="attachment_213" style="width: 564px" class="wp-caption aligncenter"><img class="size-full wp-image-213" title="ddc7csbs_12dckzpvhj_b" src="http://www.phppan.com/wp-content/uploads/2009/09/ddc7csbs_12dckzpvhj_b.png" alt="图1" width="554" height="364" /><p class="wp-caption-text">图1</p></div>
<p>2、点击确定后，在第二屏选中 【一个空的DLL工程】（An empty DLL project）, 点 【完成】Finish， 完成创建。此时会可能会弹出一个信息框，点击确定。</p>
<p>3、把解压后的vld源码中的vld文件夹下面的所有文件拷贝到到工程所在的目录（D:\php-5.2.9\ext\vld）;</p>
<p>4、选择左侧的【Source Files】，点击右键，选择【添加文件到目录】，将D:\php-5.2.9\ext\vld目录下的所有C文件添加到此目录，如图2所示;</p>
<div id="attachment_214" style="width: 305px" class="wp-caption aligncenter"><img class="size-full wp-image-214" title="ddc7csbs_13cd8tgrdg_b" src="http://www.phppan.com/wp-content/uploads/2009/09/ddc7csbs_13cd8tgrdg_b.png" alt="图2" width="295" height="281" /><p class="wp-caption-text">图2</p></div>
<p><span style="COLOR: #000000; FONT-FAMILY: 'Times New Roman'"><span style="font-size: x-small;">5、同样选择【Header Files】，添加所有的头文件到目录。如图3所示：</span></span></p>
<div id="attachment_215" style="width: 326px" class="wp-caption aligncenter"><a href="http://www.phppan.com/wp-content/uploads/2009/09/3.png"><img class="size-full wp-image-215" title="图3" src="http://www.phppan.com/wp-content/uploads/2009/09/3.png" alt="图3" width="316" height="353" /></a><p class="wp-caption-text">图3</p></div>
<p>6、修改工程设置，选择【工程】（Project）-&gt;【设置】（Settings）-&gt;【C/C++】，在【工程选项】（Project Options:）的 最后加上 /Tc，在预处理程序定义中添加如下宏定义：ZEND_DEBUG=0,COMPILE_DL_VLD,ZTS=1,ZEND_WIN32,PHP_WIN32,HAVE_VLD=1</p>
<p>如果你需要编译其它扩展，请将COMPILE_DL_VLD 和 HAVE_VLD=1，后面的 &#8220;VLD&#8221; 改成和你要创建的工程名一致。</p>
<p>如图4所示：</p>
<p style="MARGIN-LEFT: 0pt; MARGIN-RIGHT: 0pt"><span style="COLOR: #000000; FONT-FAMILY: 'Times New Roman'"><span style="font-size: x-small;"> </span></span></p>
<div>
<div id="attachment_216" style="width: 564px" class="wp-caption aligncenter"><a href="http://www.phppan.com/wp-content/uploads/2009/09/4.png"><img class="size-full wp-image-216" title="vld-4" src="http://www.phppan.com/wp-content/uploads/2009/09/4.png" alt="图4" width="554" height="370" /></a><p class="wp-caption-text">图4</p></div>
</div>
<p>7、选择【连接】（Link），在【对象/库模块】(Object/library modules)添加php5ts.lib，注意要以空格格开;</p>
<p>8、选择【工具】(Tools)-&gt;【选项】(Options)-&gt;【目录】（Directories），</p>
<p>在 【目录】（Show directories for:） 下拉框中选择 &#8220;Library files&#8221;，在 【路径】（Directories） 中添加 D:\php-5.2.9 （即 php5ts.lib 所在目录）；如图5所示：</p>
<p>在【目录】（Show directories for:） 下拉框中选择 &#8220;Include files&#8221;</p>
<p>在【路径】（Directories）中添加 D:\PHP-5.2.9 （即 ext、regex、win32 所在目录）</p>
<p>在【路径】（Directories）中添加 D:\PHP-5.2.9\MAIN</p>
<p>在【路径】（Directories）中添加D:\PHP-5.2.9\ZEND</p>
<p>在【路径】（Directories）中添加 D:\PHP-5.2.9\TSRM</p>
<p>如图6所示： </p>
<p style="TEXT-ALIGN: center"> </p>
<div id="attachment_217" style="width: 563px" class="wp-caption aligncenter"><a href="http://www.phppan.com/wp-content/uploads/2009/09/5.png"><img class="size-full wp-image-217" title="vld-5" src="http://www.phppan.com/wp-content/uploads/2009/09/5.png" alt="图5" width="553" height="389" /></a><p class="wp-caption-text">图5</p></div>
<div> </div>
<p style="TEXT-ALIGN: center"> </p>
<div id="attachment_218" style="width: 563px" class="wp-caption aligncenter"><a href="http://www.phppan.com/wp-content/uploads/2009/09/6.png"><img class="size-full wp-image-218" title="6" src="http://www.phppan.com/wp-content/uploads/2009/09/6.png" alt="图6" width="553" height="389" /></a><p class="wp-caption-text">图6</p></div>
<p style="MARGIN-LEFT: 0pt; MARGIN-RIGHT: 0pt"><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">9</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">、选择【组建】</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">-&gt;</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">【编译】，此时可能会出现报错。</span></span></p>
<p style="MARGIN-LEFT: 0pt; MARGIN-RIGHT: 0pt"><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">在</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">php_vld.h</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">文件的</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">59</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">行存在三个莫名其妙的点，将他们去掉就可以了。</span></span></p>
<p style="MARGIN-LEFT: 0pt; MARGIN-RIGHT: 0pt"><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">10</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">、选择【组建】</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">-&gt;</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">【组建】。成功！</span></span></p>
<p style="MARGIN-LEFT: 0pt; MARGIN-RIGHT: 0pt"><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">在扩展下的</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">Debug</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">目录</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">（</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">D:\php-5.2.9\ext\vld\Debug</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">）</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">下有一个生成的</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">dll</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">文件。</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">这就是我们</span></span><span style="FONT-FAMILY: 'Times New Roman'"><span style="font-size: small;">所要的东东了！</span></span></p>
<div class="mceTemp mceIEcenter"> </div>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/09/use-vc6-create-vld-extend/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
