Home mail me! Syndicate this site using RSS

Archive for ZEND API


PHP namespaces came back!

近期PHP邮件列表最引人注目的应该就是namespace的开发了,Dmitry Stogov已经为PHP6打了个补丁,不过看上去已经很完美了。具体可以点这里看他发的帖子:Simple Namespace Proposal

还记得以前PHP 5.0.0 alpha或beta的时候就已经支持namespace了,可后来推出正式版后却因为种种原因还是去掉了。现在想想namespace是越来越有用了,特别是针对Zend Framework这类框架的应用,颇有感触!希望经过这次的讨论之后会正式把这个功能加进来。
Read more…

SMART STRING API

SMART STRING API

This article was copied from http://www.akbkhome.com/blog.php/View/111/smart_str_API.html

from:

#include "ext/standard/php_smart_str.h"

The Struct:

1
2
3
4
5
typedef struct {
char *c;      // data goes here..
size_t len;     // the current length
size_t a;    // the allocated size
} smart_str;

EXAMPLE:

1
2
3
4
5
6
7
8
smart_str *sstr;
sstr = emalloc(sizeof(smart_str));
smart_str_sets(sstr, strdup("")); // start it clean.    .....
smart_str_free(sstr);
smart_str sstr = {0};
smart_str_alloc(&sstr, 100);
smart_str_sets(&sstr, strdup("This is a string"));
smart_str_free(&sstr);

Read more…

[老文章 ]使用VC++开发PHP扩展注意事项

一、默认情况下,C++是使用.cpp作为扩展名的,而PHP是用C写的,因此必须使用C++提供的C连接交换指定符号extern “C”来解决这个问题,以下两部分语句必须被包含:

extern "C" {
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
... // 其他C头文件
}

extern "C" {
#ifdef COMPILE_DL_MYEXT
ZEND_GET_MODULE(myext)
#endif
}

Read more…

[老文章]从PHP源代码修改phpinfo中的LOGO及彩蛋

1. 大家先来看一段PHP代码(gif2h.php):

< ?php
// 将GIF转换为C语言中的H文件
$filename = "mylogo.gif";
$fp = f open($filename, "rb");
$buffer = f read($fp, filesize($filename));
f close($fp);
$len = strlen($buffer);
$fp = f open("mylogo.h", "wb");
f write($fp, "unsigned char php_logo[] = {");
for ($i=0; $i<=$len; $i++) {
	if($i % 10 == 0) {
		f write($fp, "\n\t\t");
	}
	if($i == $len) {
		$str = str_pad(ord(substr($buffer, $i, 1)), 3, " ", STR_PAD_LEFT);
	} else {
		$str = str_pad(ord(substr($buffer, $i, 1)), 3, " ", STR_PAD_LEFT) . ", ";
	}
	f write($fp, $str);
}
f write($fp, " };\n");
f close($fp);
?>

上述代码的作用就是将一张GIF图片文件转换为C语言中的头文件(.h)。
Read more…

[老文章 ]PHP扩展中如何遍历数组?

PHP_FUNCTION(print_array)
{
	zval *z_array; // 外部引入的数组
	int count, i;
	zval **z_item;
	if (FAILURE == zend_parse_parameters(argc TSRMLS_CC, "a", &z_array)) {
		return;
	}
	// 获取数组大小
	count = zend_hash_num_elements(Z_ARRVAL_P(z_array));
	// 将数组的内部指针指向第一个单元
	zend_hash_internal_pointer_reset(Z_ARRVAL_P(z_array));
	for (i = 0; i < count; i++) {
		char* key;
		int idx;
		// 获取当前数据
		zend_hash_get_current_data(Z_ARRVAL_P(z_array), (void**) &z_item);
		convert_to_string_ex(z_item);
		if (zend_hash_get_current_key(Z_ARRVAL_P(z_array), &key, &idx, 0) == HASH_KEY_IS_STRING) {
			// KEY为字符串
			php_printf("array[%s] = %s", key, Z_STRVAL_PP(z_item));
		} else {
			// KEY为数字
			php_printf("array[%d] = %s", idx, Z_STRVAL_PP(z_item));
		}
		// 将数组中的内部指针向前移动一位
		zend_hash_move_forward(Z_ARRVAL_P(z_array));
	}
}

PHP调用如下:

<?php
$arr = array(
'id' => 'ezdevelop',
1 => "test number",
2 => "test number 2",
'name' => 'Wenlong Wu'
);
print_array($arr);
?>

[老文章 ]quick lesson in using stream filters

Sara Golemon在新闻组发了一篇关于在PHP扩展中如何使用stream filters的文章 ,参考如下:

If all you want to do is use an already implemented one just do this:

php_stream *stream;
php_stream_filter *filter;
zval *arguments = NULL; /* Populate this with value(s) appropriate to the filter */
 
stream = php_stream_open_wrapper(.....blah blah blah.....);
filter = php_stream_filter_create("filtername", arguments, php_stream_is_persistent(stream) TSRMLS_CC);
/* Or &stream->writefilters as appropriate */
php_stream_filter_append(&stream->readfilters, filter);

/* Of course, in the real world you’ll want to check both stream and filter for NULL as they may have failed to instantiate */

That said, I don’t think iconv.* will be any help.� It only covers
base64_(en|de)code() and quoted_printable_(en|de)code().

Filter implementation is a bit trickier.� Take a look at
ext/standard/filters.c for more info on that.� If you come up with a UTF
converter, I’m sure it can be added to the standard set of filters.

-Sara

“L0t3k” <cshmoove@xxxxxxxxxxxxx> wrote in message
news:20040823142259.47665.qmail@xxxxxxxxxxxxxxx
> can anyone give a hint as to how to use stream filters from an extension ?
>
> i have to parse input files which may be in a variety of encodings (mainly
> UTF8), and processing is done internally in UTF16.
>
> i noticed that there is an iconv filter, but i havent a clue (even after
> googling) of how to use it.
>
> l0t3k


PHP Internals – PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php