如何解决此重复名称错误?

时间:2015-03-02 11:29:10

标签: php c php-extension

我想创建一个本机原始数据类型扩展,这可以很好地进行强类型输入。我首先定义了一个名为ObjectInterface的接口,然后定义了一个名为Object的类。显然,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 个答案:

答案 0 :(得分:1)

您必须将这两个zend_function_entries与INIT_CLASS_ENTRY宏一起用作相应类条目的方法条目,并在MINIT中注册您的类。

模块结构的zend_function_entry只能有函数,而不是方法。