php4的對象
曾幾何時, 在很早的版本中, php還不支持任何的面向對象編程語法. 在php4中引入了Zend引擎(ZE1), 出現了幾個新的特性, 其中就包括對象數據類型.
php對象類型的演化
第一次的面向對象編程(OOP)支持僅實現了對象關聯的語義. 用一個php內核開發者的話來說就是"php4的對象只是將一個數組和一些方法綁定到了一起". 它就是現在你要研究的php對象.
Zend引擎(ZE2)的第二個大版本發布是在php5中, 在php的OOP實現中引入了一些新的特性. 例如, 屬性和方法可以使用訪問修飾符標記它們在你的類定義外面的可見性, 函數的重載可以用來定義內部語言結構的自定義行為, 在多個類的調用鏈之間可以使用接口實施API標准化. 在你學習到第11章"php5對象"時, 你將通過在php5的類定義中實現這些特性來建立對這些知識的認知.
實現類
在進入OOP的世界之前, 我們需要輕裝上陣. 因此, 請將你的擴展恢復到第5章"你的第一個擴展"中剛剛搭建好的骨架形態.
為了和你原有的習作獨立, 你可以將這個版本命名為sample2. 將下面的三個文件放入到你php源代碼的ext/sample2目錄下:
config.m4
PHP_ARG_ENABLE(sample2,
[Whether to enable the "sample2" extension],
[ enable-sample2 Enable "sample2" extension support])
if test $PHP_SAMPLE2 != "no"; then
PHP_SUBST(SAMPLE2_SHARED_LIBADD)
PHP_NEW_EXTENSION(sample2, sample2.c, $ext_shared)
fi
php_saple2.h
#ifndef PHP_SAMPLE2_H
/* Prevent double inclusion */
#define PHP_SAMPLE2_H
/* Define Extension Properties */
#define PHP_SAMPLE2_EXTNAME "sample2"
#define PHP_SAMPLE2_EXTVER "1.0"
/* Import configure options
when building outside of
the PHP source tree */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* Include PHP Standard Header */
#include "php.h"
/* Define the entry point symbol
* Zend will use when loading this module
*/
extern zend_module_entry sample2_module_entry;
#define phpext_sample2_ptr &sample2_module_entry
#endif /* PHP_SAMPLE2_H */
sample2.c
#include "php_sample2.h"
static function_entry php_sample2_functions[] = {
{ NULL, NULL, NULL }
};
PHP_MINIT_FUNCTION(sample2)
{
return SUCCESS;
}
zend_module_entry sample2_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_SAMPLE2_EXTNAME,
php_sample2_functions,
PHP_MINIT(sample2),
NULL, /* MSHUTDOWN */
NULL, /* RINIT */
NULL, /* RSHUTDOWN */
NULL, /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
PHP_SAMPLE2_EXTVER,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_SAMPLE2
ZEND_GET_MODULE(sample2)
#endif
現在, 就像在第5章時一樣, 你可以執行phpize, ./configure, make去構建你的sample2.so擴展模塊.
你之前的config.w32做與這裡給出的config.m4一樣的修改也可以正常工作.