In Java, I can pass an object straight in parameter
public int foo (Bar bar)
{
... call the methods from Bar class
}
So how can I do same thing with PHP. Thanks
This is my code:
class Photo
{
private $id, $name, $description;
public function Photo($id, $name, $description)
{
$this->id = $id;
$this->name = $name;
$this->description = $description;
}
}
class Photos
{
private $id = 0;
private $photos = array();
private function add(Photo $photo)
{
array_push($this->photos, $photo);
}
public function addPhoto($name, $description)
{
add(new Photo(++$this->id, $name, $description));
}
}
$photos = new Photos();
$photos->addPhoto('a', 'fsdfasd');
var_dump($photos); // blank
If I change the function add
function add($name, $description)
{
array_push($this->photos, new Photo(++$this->id, $name, $description));
}
It works pefectly. So What is wrong ?
You do it the exact same way, just with some syntax changes, of course:
Keep in mind that you can only type hint for classes and arrays.