我试图学习Facade模式,但遇到了问题。 PHP不会从PrintPrice方法输出$ totalprice。另外,这里有附加信息:初始化后的var_dump和对象给我:
private 'SkiRent' =>
object(SkiRent)[2]
private 'SkiResortTicketSystem' =>
object(SkiResortTicketSystem)[3]
private 'HotelBookingSystem' =>
object(HotelBookingSystem)[4]
private 'totalprice' => null
但调用HaveGoodRest后的var_dump给了我空白页面。 我想知道你是否可以帮助我,这是代码:
<?php
class SkiRent
{
public function RentBoots($feetSize, $skierLevel)
{
return 20*$skierLevel*100/$feetSize;
}
public function RentSki($weight, $skierLevel)
{
return 40*$skierLevel*100/$weight;
}
public function RentPole($height)
{
return 5*$height/100;
}
};
class SkiResortTicketSystem
{
public function BuyOneDayTicket()
{
return 115;
}
public function BuyHalfDayTicket()
{
return 60;
}
};
class HotelBookingSystem
{
public function BookRoom($roomQuality)
{
switch ($roomQuality)
{
case 3:
return 250;
case 4:
return 500;
case 5:
return 900;
default:
throw new ArgumentException("roomQuality should be in [3;5]");
}
}
};
class SkiResortFacade
{
private $SkiRent;
private $SkiResortTicketSystem;
private $HotelBookingSystem;
private $totalprice;
function __construct()
{
$this->SkiRent = new SkiRent();
$this->SkiResortTicketSystem = new SkiResortTicketSystem();
$this->HotelBookingSystem = new HotelBookingSystem();
}
function HaveGoodRest($height, $weight, $feetSize, $skierLevel, $roomQuality)
{
$skiPrice = $SkiRent->RentSki($weight, $skierLevel);
var_dump($skiPrice);
$skiBootsPrice = $SkiRent->RentBoots($feetSize,$skierLevel);
$polePrice = $SkiRent->RentPole($height);
$oneDayTicketPr = $SkiResortTicketSystem->BuyOneDayTicket();
$hotelPrice = $HotelBookingSystem->BookRoom($roomQuality);
$this->totalprice = $skiPrice + $skiBootsPrice + $polePrice + $oneDayTicketPr + $hotelPrice;
}
public function HaveRestWithOwnSkis($roomQuality)
{
$oneDayTicketPr = $SkiResortTicketSystem->BuyOneDayTicket();
$hotelPrice = $HotelBookingSystem->BookRoom($roomQuality);
return $oneDayTicketPr + $hotelPrice;
}
public function PrintPrice()
{
echo $this->totalprice;
}
};
$rest = new SkiResortFacade();
var_dump($rest);
$height = 181.5;
$weight = 70.1;
$feetSize = 45.2;
$skierLevel = 1.2;
$roomQuality = 3;
$rest->HaveGoodRest($height, $weight, $feetSize, $skierLevel, $roomQuality);
var_dump($rest);
$rest->PrintPrice();
?>
答案 0 :(得分:2)
致命错误:在第65行的非对象上调用成员函数RentSki()
$skiPrice = $SkiRent->RentSki($weight, $skierLevel); // Line 65
$SkiRent
不是对象,因为您没有实例化它或将其作为HaveGoodRest()
方法中的参数传递
您对代码中的非对象进行了多次调用。
答案 1 :(得分:0)
您已在SkiResortFacade类中定义了$ this-&gt; SkiRent属性:
$this->SkiRent = new SkiRent();
但你没有使用$ this引用它:
$skiPrice = $SkiRent->RentSki($weight, $skierLevel);
应该是:
$skiPrice = $this->SkiRent->RentSki($weight, $skierLevel);
这是一个有效的代码,因此您可以比较您的版本:
<?php
class SkiRent
{
public function RentBoots($feetSize, $skierLevel)
{
return 20*$skierLevel*100/$feetSize;
}
public function RentSki($weight, $skierLevel)
{
return 40*$skierLevel*100/$weight;
}
public function RentPole($height)
{
return 5*$height/100;
}
};
class SkiResortTicketSystem
{
public function BuyOneDayTicket()
{
return 115;
}
public function BuyHalfDayTicket()
{
return 60;
}
};
class HotelBookingSystem
{
public function BookRoom($roomQuality)
{
switch ($roomQuality)
{
case 3:
return 250;
case 4:
return 500;
case 5:
return 900;
default:
throw new ArgumentException("roomQuality should be in [3;5]");
}
}
};
class SkiResortFacade
{
private $SkiRent;
private $SkiResortTicketSystem;
private $HotelBookingSystem;
private $totalprice;
function __construct()
{
$this->SkiRent = new SkiRent();
$this->SkiResortTicketSystem = new SkiResortTicketSystem();
$this->HotelBookingSystem = new HotelBookingSystem();
}
function HaveGoodRest($height, $weight, $feetSize, $skierLevel, $roomQuality)
{
$skiPrice = $this->SkiRent->RentSki($weight, $skierLevel);
var_dump($skiPrice);
$skiBootsPrice = $this->SkiRent->RentBoots($feetSize,$skierLevel);
$polePrice = $this->SkiRent->RentPole($height);
$oneDayTicketPr = $this->SkiResortTicketSystem->BuyOneDayTicket();
$hotelPrice = $this->HotelBookingSystem->BookRoom($roomQuality);
$this->totalprice = $skiPrice + $skiBootsPrice + $polePrice + $oneDayTicketPr + $hotelPrice;
}
public function HaveRestWithOwnSkis($roomQuality)
{
$oneDayTicketPr = $SkiResortTicketSystem->BuyOneDayTicket();
$hotelPrice = $HotelBookingSystem->BookRoom($roomQuality);
return $oneDayTicketPr + $hotelPrice;
}
public function PrintPrice()
{
echo $this->totalprice;
}
};
$rest = new SkiResortFacade();
$height = 181.5;
$weight = 70.1;
$feetSize = 45.2;
$skierLevel = 1.2;
$roomQuality = 3;
$rest->HaveGoodRest($height, $weight, $feetSize, $skierLevel, $roomQuality);
$rest->PrintPrice();