PHP: 不能使用继承。它找不到 class
PHP: Can't use inheritance. It won't find the class
我达到了关于继承的上限,但我无法使用它们,即使我尝试使用我正在学习的书中的示例也是如此。即使所有文件都在同一个文件夹中,错误是:
"Fatal error: Class 'mother' not found in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\Learning\classes\son.php on line 2"
让我举一个例子来解释。
文件: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();
?>
结果:
ERROR: Fatal error: Class 'mother' not found in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\Learning\classes\son.php on line 2
为什么会这样?如果 class 在同一个文件夹中,为什么它找不到?!
您还需要包括 mother.php
。否则它无法找到 class 作为错误状态。
天真的例子:
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();
}
}
?>
我达到了关于继承的上限,但我无法使用它们,即使我尝试使用我正在学习的书中的示例也是如此。即使所有文件都在同一个文件夹中,错误是:
"Fatal error: Class 'mother' not found in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\Learning\classes\son.php on line 2"
让我举一个例子来解释。
文件: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();
?>
结果:
ERROR: Fatal error: Class 'mother' not found in C:\Program Files (x86)\EasyPHP-Devserver-16.1\eds-www\Learning\classes\son.php on line 2
为什么会这样?如果 class 在同一个文件夹中,为什么它找不到?!
您还需要包括 mother.php
。否则它无法找到 class 作为错误状态。
天真的例子:
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();
}
}
?>