「PHP」抽象クラスと抽象メソッドのサンプル

書式
abstract class クラス名 {
abstract function メソッド名
}
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
//抽象クラス
abstract class Sample
{
abstract function getMessage();
}
//抽象クラスをextendsで継承
class Child extends Sample
{
public function getMessage(){
return "user 007";
}
}
$gt = new Child();
echo "result 1234" . "<br>";
print $gt->getMessage();
?>
<?php //抽象クラス abstract class Sample { abstract function getMessage(); } //抽象クラスをextendsで継承 class Child extends Sample { public function getMessage(){ return "user 007"; } } $gt = new Child(); echo "result 1234" . "<br>"; print $gt->getMessage(); ?>
<?php
//抽象クラス
abstract class Sample 
{
      abstract function getMessage();
}
//抽象クラスをextendsで継承
class Child extends Sample 
{
      public function getMessage(){
            return "user 007";
      }
}
$gt = new Child();
 echo "result 1234" . "<br>";
print $gt->getMessage(); 
?>

実行結果
result 1234
user 007

PHP

Posted by arkgame