在
Java中,我可以直接在参数中传递一个对象
public int foo (Bar bar)
{
... call the methods from Bar class
}
那么我怎样才能用PHP做同样的事情.谢谢
这是我的代码:
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
如果我更改功能添加
function add($name, $description)
{
array_push($this->photos, new Photo(++$this->id, $name, $description));
}
它完美地运作.那有什么不对?