我是php新手,请原谅我缺乏知识。我正在使用eclipse并在项目中有一个包含3个文件的项目。我正在创建一个find折扣类,它使类对象从另一个类调用一个函数。错误:
注意:未定义的变量:GetInfoClass行.. 致命错误:在非对象行上调用成员函数getAge()...
我试着读一下它,但我似乎无法理解它。请帮忙。感谢
formResponse:
include "GetInfo.php";
include "IfDiscount.php";
$IfDiscount= new IfDiscount();
echo $IfDiscount->findDiscount();
类IfDiscount:
class IfDiscount
{
public function findDiscount(){
$Age = $GetInfoClass->getAge();
echo $Age;}}
答案 0 :(得分:1)
$GetInfoClass
无法使用 findDiscount()
,因为它超出了范围。您应该将其作为参数传递给findDiscount()
,以使其可用于该方法:
public function findDiscount($GetInfoClass){
$Age = $GetInfoClass->getAge();
return $Age;
}
echo $IfDiscount->findDiscount($GetInfoClass);
(你也想返回 $Age
,而不是回应它。当你调用那个方法时,你已经明确地回应了它。)