我有一个对象可以提取有关该站点的详细信息。基本上是名称地址等。它撤回的字段之一是 IDCounty。见下文。然后我想将它输入到另一个对象的新实例中,它会自动给我县名。这是我所拥有的
所以我的系统对象
class System{
private $db;
public $Ad1;
public $Ad2;
public $County;
public $IDSystem;
//connect to database
public function __construct($IDSystem ='1') {
$this->db = new Database();
$this->IDSystem = $IDSystem;
}
//get address
public function ContactInfo() {
$Query = sprintf("SELECT BSAd1, BSAd2, IDCounty FROM BusinessSettings WHERE IDBusinessSettings = %s",
GetSQLValueString($this->IDSystem, "int"));
$Query = $this->db->query($Query);
$Result = $this->db->fetch_assoc($Query);
$this->Ad1 = $Result['BSAd1'];
$this->Ad2 = $Result['BSAd2'];
$County = new County($Result['IDCounty']);
$this->County = $County;
}
//end address
}
如您所见,该对象正在调用另一个县并将 $County 设置为 $this->County。我的详细信息如下
class County {
public $IDCounty;
private $db;
public function __construct($IDCounty) {
$this->db = new Database();
$this->IDCounty = $IDCounty;
$Query = sprintf("SELECT CountyName FROM County WHERE IDCounty = %s", GetSQLValueString($this->IDCounty, "int"));
$Query = $this->db->query($Query);
$County = $this->db->fetch_assoc($Query);
return $County['CountyName'];
}
}
当我调用对象时,我这样称呼它
$SiteDetails = new System();
$SiteDetails->ContactInfo();
echo $SiteDetails->Ad1;
echo $SiteDetails->County;
我从 echo $SiteDetails->County 的错误报告中得到一个错误;上面写着“可捕获的致命错误:County 类的对象无法转换为字符串”
谷歌搜索此错误后,我看到 System 类在转换从 County 类获取县名并将其转换为 $this->County 时遇到问题
对我来说不幸的是,我不确定如何解决它。我以为我可以在实例化时从函数返回一个值,但似乎我错了。请帮忙。多谢你们。