繁体   English   中英

PHP:不能使用继承。 它不会找到班级

[英]PHP: Can't use inheritance. It won't find the class

我达到了关于继承的限制但我不能使用它们,即使我试图使用我正在学习的书中的例子。 即使所有文件都在同一文件夹中,错误仍然是:

“致命错误:在第2行的C:\\ Program Files(x86)\\ EasyPHP-Devserver-16.1 \\ eds-www \\ Learning \\ classes \\ son.php中找不到类'母亲'”

让我展示一下我创建的例子来解释。

文件: mother.php

    <?php
    class mother
    {
       public $word= "Hello!!!";

       function printWord()
       {
         echo $word;   
       }
     }
     ?>

文件: son.php

<?php 
  class son extends mother
   {
     function printWord()
     {
      parent::printWord();
     }
   }  
?>

文件: test.php

<?php
include 'son.php';
$test = new son();
$test->printWord();
?>

结果是:

错误:致命错误:在第2行的C:\\ Program Files(x86)\\ EasyPHP-Devserver-16.1 \\ eds-www \\ Learning \\ classes \\ son.php中找不到类'mother'

为什么会这样? 如果它在同一个文件夹中,为什么它找不到该类?

你也需要包括mother.php 否则它无法找到类作为错误状态。

天真的例子:

test.php的

<?php
include 'mother.php'
include 'son.php';
$test = new son();
$test->printWord();
?>

但是甚至有更好的方法

son.php

<?php 
  require_once 'mother.php'
  class son extends mother
   {
     function printWord()
     {
      parent::printWord();
     }
   }  
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM