Daniel Robinson
Daniel Robinson

Reputation: 653

PHP Object how to call an object from another object

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.

Upvotes: 0

Views: 141

Answers (2)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

Whatever return statements a constructor may contain, they will be ignored. A constructor will, by definition, return a new instance of a given class. Read the docs. Though the signature of __construct() shows a return-type of void, you should think of it as void* (a void pointer). That means, it returns a reference to the newly created object. You should replace the return statement with soimething like:

$this->stringValue = $County['CountyName'];

And, because of many other magic-methods, you can implement another one to use a class as a string: the magic __toString method:

public function __toString()
{
    return (string) $this->stringValue;//the cast is optional
}

As ever, read through the documentation on some of the other magic-methods. They're quite useful, though I must add, they are/can be slow. you could, equally well implement a getter:

//assuming this is in your constructor:
$this->stringValue = $County['CountyName'];

public function getContyName($default = '')
{
    if (!$this->stringValue)
    {
        return $default;
    }
    return $this->stringValue;
}

Now, thanks to the $default argument, I can do this:

$county = new County();//not passing any values
echo $county->getCountyName('unknown');//will echo unknown
echo $county->getCountyName();//echoes empty string, because that's the default value of $default...
$county2 = new County('Foobar');
echo $county2->getCountyName('unknown');//echoes Foobar, county is known...

Other than that, your sprintf call isn't making much sense to me. The format used is %s, so I'd expect you pass a string. In fact you're passing GetSQLValueString($this->IDCounty, "int"), which is a the return value of a call to a global function (terrible code smell BTW). Since you're passing 'int' as an argument there, I'd expect the return value to be an int, so why not %d in your sprintf format?
There are many, many other things that need work here (for example: creating a new Database instance without params? really? Why not query using a DB object? why use a global function? Why aren't you using prepared statements? ... Please, read up some more on OOP

Upvotes: 1

Reinherd
Reinherd

Reputation: 5506

Implement the method __toString() in County class. Example:

public function __toString()
{
    return $this->foo;
}

Upvotes: 0

Related Questions