I'm fairly new to object orientated programming, so bear with me if I've missed something simple, or done something wrong.
I have a set up where the domains class can contain details on a domain name (such as name, expiry, creation date etc.), but can also have a $hosting variable which relates to the hosting account that the domain is tied with. However, not all domains are hosted and are just there waiting to be used, so the hosting object doesn't always exist.
In both domains and hosting I have functions to return the relevant details, so for example:
private $accountId;
private $name;
private $created;
public function getAccountId() { return $this->accountId; }
public function getName() { return $this->name; }
public function getCreated() { return $this->created; }
So, if I wanted the hosting accounts id from within the domain object (called $domain) I could do:
$domain->getHosting()->getId();
Hopfully that makes sense!
If there is a hosting account, the ID is returned:
PHP Fatal error: Call to a member function getAccountId() on a non-object in /home/sites/.../file.php on line x
Is there a way of checking if $domain->getHosting()
exists to prevent this error?
Many Thanks in advance, and sorry if this is a simple error!