PHP 소스 코드 29에 대한 간략한 토론: 인터페이스 상속에 대하여

不言
풀어 주다: 2023-04-02 06:30:02
원래의
2109명이 탐색했습니다.

이 글은 주로 PHP 소스 코드 29에 대한 간단한 토론을 소개합니다. 인터페이스 상속과 관련하여 특정 참조 가치가 있습니다. 이제 필요한 친구가 참조할 수 있도록 공유합니다.

PHP 소스에 대한 간단한 토론 code 29: 인터페이스 상속에 대하여

이전에 PHP 소스 코드에서 클래스 상속을 본 적이 있습니다. 오늘은 PHP에서 인터페이스 상속이 어떻게 구현되는지 살펴보겠습니다.
마찬가지로 CachingIterator 클래스에서 시작하여 인터페이스의 상속된 구현을 찾습니다.
CachingIterator 확장 IteratorIterator는 OuterIterator, Traversable, Iterator, ArrayAccess, Countable을 구현합니다.

  /* ArrayAccess接口的继承实现 */REGISTER_SPL_IMPLEMENTS(CachingIterator, ArrayAccess); //    ext/spl/spl_functions.h 41行#define REGISTER_SPL_IMPLEMENTS(class_name, interface_name) \
zend_class_implements(spl_ce_ ## class_name TSRMLS_CC, 1, spl_ce_ ## interface_name); //zend/zend_API.c 2218行ZEND_API void zend_class_implements(zend_class_entry *class_entry TSRMLS_DC, int num_interfaces, ...) /* {{{ */{
zend_class_entry *interface_entry;
va_list interface_list;
va_start(interface_list, num_interfaces); while (num_interfaces--) {
interface_entry = va_arg(interface_list, zend_class_entry *);
zend_do_implement_interface(class_entry, interface_entry TSRMLS_CC);
} va_end(interface_list);}/* }}} */ 
 
 //zend/zend_complie.c 2887行ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry *iface TSRMLS_DC) /* {{{ */{
zend_uint i, ignore = 0;
zend_uint current_iface_num = ce->num_interfaces;
zend_uint parent_iface_num  = ce->parent ? ce->parent->num_interfaces : 0; for (i = 0; i < ce->num_interfaces; i++) {
if (ce->interfaces[i] == NULL) {
memmove(ce->interfaces + i, ce->interfaces + i + 1, sizeof(zend_class_entry*) * (--ce->num_interfaces - i));
i--;
} else if (ce->interfaces[i] == iface) {/* 已存在此接口 */
if (i < parent_iface_num) {
ignore = 1;
} else {
zend_error(E_COMPILE_ERROR, "Class %s cannot implement previously implemented interface %s", ce->name, iface->name);
}
}
}
if (!ignore) {
if (ce->num_interfaces >= current_iface_num) {
if (ce->type == ZEND_INTERNAL_CLASS) {
ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
} else {
ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
}
}
ce->interfaces[ce->num_interfaces++] = iface;//接口数加1,将接口加入接口列表中 /* 合并接口中的常量列表和方法列表 */
zend_hash_merge_ex(&ce->constants_table, &iface->constants_table, (copy_ctor_func_t) zval_add_ref, sizeof(zval *), (merge_checker_func_t) do_inherit_constant_check, iface);
zend_hash_merge_ex(&ce->function_table, &iface->function_table, (copy_ctor_func_t) do_inherit_method, sizeof(zend_function), (merge_checker_func_t) do_inherit_method_check, ce); 
do_implement_interface(ce, iface TSRMLS_CC);
zend_do_inherit_interfaces(ce, iface TSRMLS_CC);
}}
로그인 후 복사

zend_do_implement_interface 함수의 병합된 인터페이스에 있는 상수 목록 및 메서드 목록 작업을 보면 이것이 변수가 있을 수 없는 이유 중 하나일 수 있다고 추측할 수 있습니다. 하지만 인터페이스의 상수
인터페이스 상속 과정에서 현재 클래스의 인터페이스에 동일한 인터페이스가 있는지 여부에 대한 판단 작업이 있습니다. 동일한 인터페이스가 이미 존재하는 경우 해당 인터페이스는 상속되지 않습니다.
php 코드는 다음과 같습니다.

<?phpinterface FooInterface {
public function method1();} class Base implements FooInterface {
public function method1() {
echo &#39;ss&#39;;
}} class Foo extends Base implements FooInterface { } $foo = new Foo();$foo->method1();
로그인 후 복사

위 내용은 모두의 학습에 도움이 되기를 바랍니다. 더 많은 관련 내용은 PHP 중국어 홈페이지를 주목해주세요!

관련 권장 사항:

PHP 소스 코드 28에 대한 간략한 토론: 클래스 구조 및 상속 정보

PHP 소스 코드 27에 대한 간략한 토론: PHP의 구성 방법 식별

PHP 소스 코드 2에 대한 간략한 논의 16: PHP 퀵 정렬 소스 코드 구현 단순화

위 내용은 PHP 소스 코드 29에 대한 간략한 토론: 인터페이스 상속에 대하여의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!