天天看點

php源碼解析:繼承與實作接口

在定義一個類時往往會使其繼承某個父類或者實作某個接口,在擴充中實作這個功能非常友善。下面我先給出PHP語言中的代碼。

<?php
interface i_myinterface
{
	public function hello();
}

class parent_class implements i_myinterface
{
	public function hello()
	{
		echo "Good Morning!\n";
	}
}

final class myclass extends parent_class
{
	public function call_hello()
	{
		$this->hello();
	}
}
?>
           

上面的代碼我們已經非常熟悉了,它們在PHP擴充中的實作應該是這樣的:

//三個zend_class_entry
zend_class_entry *i_myinterface_ce,*parent_class_ce,*myclass_ce;

//parent_class的hello方法
ZEND_METHOD(parent_class,hello)
{
	php_printf("hello world!\n");
}

//myclass的call_hello方法
ZEND_METHOD(myclass,call_hello)
{
	//這裡涉及到如何調用對象的方法,詳細内容下一章叙述
	zval *this_zval;
	this_zval = getThis();
	zend_call_method_with_0_params(&this_zval,myclass_ce,NULL,"hello",NULL);
}

//各自的zend_function_entry
static zend_function_entry i_myinterface_method[]={
	ZEND_ABSTRACT_ME(i_myinterface,	hello,	NULL)
	{NULL,NULL,NULL}
};

static zend_function_entry parent_class_method[]={
	ZEND_ME(parent_class,hello,NULL,ZEND_ACC_PUBLIC)
	{NULL,NULL,NULL}
};

static zend_function_entry myclass_method[]={
	ZEND_ME(myclass,call_hello,NULL,ZEND_ACC_PUBLIC)
	{NULL,NULL,NULL}
};

ZEND_MINIT_FUNCTION(test)
{
	zend_class_entry ce,p_ce,i_ce;
	INIT_CLASS_ENTRY(i_ce,"i_myinterface",i_myinterface_method);
	i_myinterface_ce = zend_register_internal_interface(&i_ce TSRMLS_CC);
	
	
	//定義父類,最後使用zend_class_implements函數聲明它實作的接口
	INIT_CLASS_ENTRY(p_ce,"parent_class",parent_class_method);
	parent_class_ce = zend_register_internal_class(&p_ce TSRMLS_CC);
	zend_class_implements(parent_class_ce TSRMLS_CC,1,i_myinterface_ce);

	//定義子類,使用zend_register_internal_class_ex函數
	INIT_CLASS_ENTRY(ce,"myclass",myclass_method);
	myclass_ce = zend_register_internal_class_ex(&ce,parent_class_ce,"parent_class" TSRMLS_CC);
	//注意:ZEND_ACC_FINAL是用來修飾方法的,而ZEND_ACC_FINAL_CLASS是用來修飾類的
	myclass_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
	return SUCCESS;
}
           

這樣,當我們在PHP語言中進行如下操作時,便會得到預期的輸出:

<?php
$obj = new myclass();
$obj->hello();
/*
輸出内容:
hello world!
*/
?>
           

這裡的ZEND_ABSTRACT_ME()宏函數比較特殊,它會聲明一個abstract public類型的函數,這個函數不需要我們實作,是以也就不需要相應的ZEND_METHOD(i_myinterface,hello){...}的實作。一般來說,一個接口是不能設計出某個非public類型的方法的,因為接口暴露給使用者的都應該是一些公開的資訊。不過如果你非要這麼設計,那也不是辦不到,隻要别用ZEND_ABSTRACT_ME()宏函數就行了,而用它的底層實作ZEND_FN()宏函數。

//它可以對應<?php ...public static function apply_request();...的接口方法聲明。
static zend_function_entry i_myinterface[]=
{
	ZEND_FENTRY(apply_request, NULL, NULL, ZEND_ACC_STATIC|ZEND_ACC_ABSTRACT|ZEND_ACC_PUBLIC)
	{NULL,NULL,NULL}
};
           

這樣,隻要掩碼中有ZEND_ACC_ABSTRACT,便代表是一個不需要具體實作的方法。ZEND_FENTRY其實是ZEND_ME和ZEND_FE的最終實作,現在我們把這一組宏羅列在這一次展開,供你參考使用。

#define ZEND_FENTRY(zend_name, name, arg_info, flags)	{ #zend_name, name, arg_info, (zend_uint) (sizeof(arg_info)/sizeof(struct _zend_arg_info)-1), flags },

#define ZEND_FN(name) zif_##name

#define ZEND_MN(name) zim_##name

#define ZEND_FE(name, arg_info)						ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)

#define ZEND_ME(classname, name, arg_info, flags)	ZEND_FENTRY(name, ZEND_MN(classname##_##name), arg_info, flags)