我达到了关于继承的限制但我不能使用它们,即使我尝试使用我正在学习的书中的例子。即使所有文件都在同一文件夹中,错误仍是:
“致命错误:在第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中找不到类'母亲'
为什么会这样?如果它在同一个文件夹中,为什么它找不到该类?
答案 0 :(得分:3)
您还需要包含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();
}
}
?>