2015-03-02 58 views
2

我想創建一個原生的原始數據類型擴展,這對於強打字很有用。我首先定義了一個名爲ObjectInterface的接口,然後是一個名爲Object的類。顯然,對象實現ObjectInterface,我有這個下面的代碼:如何解決這個重複名稱錯誤?

/* {{{ scalarclass_functions[] 
* 
* Every user visible function must have an entry in scalarclass_functions[]. 
*/ 
const zend_function_entry scalarclass_functions[] = { 
     PHP_ABSTRACT_ME(ObjectInterface, equals, arginfo_object) 
     PHP_ABSTRACT_ME(ObjectInterface, getClass, arginfo_void) 
     PHP_ABSTRACT_ME(ObjectInterface, getClassName, arginfo_void) 
     PHP_ABSTRACT_ME(ObjectInterface, hashCode, arginfo_void) 
     PHP_ABSTRACT_ME(ObjectInterface, __toString, arginfo_void) 
     PHP_ME(Object, equals, arginfo_object, ZEND_ACC_PUBLIC) 
     PHP_ME(Object, getClass, arginfo_void, ZEND_ACC_PUBLIC) 
     PHP_ME(Object, getClassName, arginfo_void, ZEND_ACC_PUBLIC) 
     PHP_ME(Object, hashCode, arginfo_void, ZEND_ACC_PUBLIC) 
     PHP_ME(Object, __toString, arginfo_void, ZEND_ACC_PUBLIC) 
     PHP_FE_END  /* Must be the last line in scalarclass_functions[] */ 
}; 

當然,這只是部分的代碼,我沒有表現出對對象類的方法,該方法定義,因爲該代碼可以是有點長,分散注意力。然後我遇到了一個問題,它給了我對象類方法的重複名稱錯誤。

PHP Warning: Function registration failed - duplicate name - equals in Unknown on line 0 
PHP Warning: Function registration failed - duplicate name - getClass in Unknown on line 0 
PHP Warning: Function registration failed - duplicate name - getClassName in Unknown on line 0 
PHP Warning: Function registration failed - duplicate name - hashcode in Unknown on line 0 
PHP Warning: Function registration failed - duplicate name - __toString in Unknown on line 0 

看來,編譯器不能告訴的方法Object::equals()ObjectInterface::equals()有不同的方法,而是假定它們是相同的,並拋出一個重複名稱的警告。我該如何解決這個問題?

我試圖定義第二個zend_function_entry結構,但它不會工作,因爲編譯器會忽略第二個zend_function_entry。我看到PHP的內置DateTime和Reflection擴展能夠解決這個問題,但這可能如何?

回答

1

您必須將這兩個zend_function_entries與INIT_CLASS_ENTRY宏一起用作各個類條目的方法條目,並在MINIT中註冊您的類。

模塊struct的zend_function_entry只能有函數而不是方法。