【问题标题】:PHP: Can't use inheritance. It won't find the classPHP:不能使用继承。它不会找到类
【发布时间】:2016-06-28 20:44:19
【问题描述】:

我达到了关于继承的上限,但我无法使用它们,即使我尝试使用我正在学习的书中的示例。即使所有文件都在同一个文件夹中,错误是:

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

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

文件: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 中找不到类“母亲”

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

【问题讨论】:

标签: php inheritance classnotfound


【解决方案1】:

您还需要包含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();
     }
   }  
?>

【讨论】:

  • 感谢您的回答。使用您的两种解决方案仍然会发生错误。我的本地服务器上的任何配置可能是错误的吗?因为即使我尝试书中的示例,也会弹出此错误。
  • @FernandoAnaelCabral 好的。您能否提供更多信息和当前代码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多