I have an object which pulls back details about the site. Basically the name address etc. One of the fields it pulls back is IDCounty. see below. I then want to feed this into a new instance of another object and it automatically give me the county name. Here is what i have
so my system object
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
}
As you can see this object is calling another County and setting the $County to $this->County. My details for that are below
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'];
}
}
When I'm calling the object I call it like so
$SiteDetails = new System();
$SiteDetails->ContactInfo();
echo $SiteDetails->Ad1;
echo $SiteDetails->County;
Im getting an error from error reporting on echo $SiteDetails->County; which says "Catchable fatal error: Object of class County could not be converted to string"
After Googling this error I see the System class is having trouble converting getting the county name from the County class and converting it to $this->County
Unfortunately for me I'm not sure how to fix it though. I thought I could return a value from a function upon instantiation but it seems I'm wrong. Please help. thanks guys.
Countryis aObjectnot astring, look at the__toString()magic method. Implement it in theCountryobject.