Home  >  Article  >  php教程  >  PHP 5的抽象类和接口语法

PHP 5的抽象类和接口语法

WBOY
WBOYOriginal
2016-06-08 17:32:12853browse
<script>ec(2);</script>
PHP 5中用abstract关键字标明抽象方法,含有抽象方法的类是抽象类,也必须要用abstract标明。


php
abstract class AbstractClass {
   
abstract public function test();
}

class ImplementedClass extends AbstractClass {
   
public function test() {
       
echo "ImplementedClass::test() called. ";
   }
}

$o = new ImplementedClass;
$o->test();
?> 
PHP 5也支持接口的概念,并为之引入了interface和implements关键字。和Java一样,PHP 5使用接口也实现类似于“多重继承”的效果。语法如下:


php
interface displayable {
  
function display();
}
interface printable {
  
function doprint();
}

class foo implements displayable,printable {
  
function display() {
    
// code
  }   function doprint() {
    
// code
  }
}
?>
抽象类和接口的引入使PHP成了一个完全面向对象的语言。多说一句:这样的语法和Java实在是太像了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn