>  기사  >  백엔드 개발  >  PHP에서 가상 메소드를 구현하는 방법

PHP에서 가상 메소드를 구현하는 방법

藏色散人
藏色散人원래의
2020-10-09 09:53:471850검색

Php 가상 메소드 구현: 먼저 PHP 샘플 파일을 생성한 다음 "e6b359aafb47ca7cdd35a7d1828461f4x()를 사용하여 A::x()를 호출하면 하위 클래스의 A::y()가 :: x()는 B::x()에 의해 재정의되고 A::y()는 B::x()를 호출합니다.

위 예제의 실행 결과는 다음과 같습니다.

A::x() was called. A::y() was called. --
B::x() was called. A::z() was called.
virtual-function.php

코드는 다음과 같습니다.

<?php 
class ParentClass { 
static public function say( $str ) { 
static::do_print( $str ); 
} 
static public function do_print( $str ) { 
echo "<p>Parent says $str</p>"; 
} 
} 
class ChildClass extends ParentClass { 
static public function do_print( $str ) { 
echo "<p>Child says $str</p>"; 
} 
} 
class AnotherChildClass extends ParentClass { 
static public function do_print( $str ) { 
echo "<p>AnotherChild says $str</p>"; 
} 
} 
echo phpversion(); 
$a=new ChildClass(); 
$a->say( &#39;Hello&#39; ); 
$b=new AnotherChildClass(); 
$b->say( &#39;Hello&#39; );

위 내용은 PHP에서 가상 메소드를 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.