Skip to content

Instantly share code, notes, and snippets.

@dingsda87
Created July 2, 2014 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dingsda87/7866bd20bfdecb544ca7 to your computer and use it in GitHub Desktop.
Save dingsda87/7866bd20bfdecb544ca7 to your computer and use it in GitHub Desktop.
<?php
class Foo{
protected $bar;
/** @var Lol[] */
protected $lols=array();
public function setBar(Bar $bar){
$this->bar=$bar;
}
public function getBar(){
return $this->bar;
}
public function addLol(Lol $lol){
$this->lols[]=$lol;
}
/**
* @return Lol[]
*/
public function getAllLols(){
return $this->lols;
}
}
class Bar{
protected $id;
public function setID($id){
$this->id=$id;
}
}
class Lol{
protected $name;
/** @var Bar */
protected $bar;
public function setBar(Bar $bar){
$this->bar=$bar;
}
public function getBar(){
return $this->bar;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$bar=new Bar();
$bar->setID(1);
var_dump($bar);
// object(Bar)[1]
// protected 'id' => int 1
$foo=new Foo();
$foo->setBar($bar);
$bar->setID(3);
var_dump($foo);
// object(Foo)[2]
// protected 'bar' =>
// object(Bar)[1]
// protected 'id' => int 3
// protected 'lols' =>
// array (size=0)
// empty
var_dump($bar);
// object(Bar)[1]
// protected 'id' => int 3
var_dump($bar===$foo->getBar());
// boolean true
$newFoo=new Foo();
for($i=1; $i<5; $i++){
$newlol=new Lol();
$newbar=new Bar();
$newbar->setID($i);
$newlol->setBar($newbar);
$newFoo->addLol($newlol);
}
var_dump($newFoo);
// object(Foo)[3]
// protected 'bar' => null
// protected 'lols' =>
// array (size=4)
// 0 =>
// object(Lol)[4]
// protected 'name' => null
// protected 'bar' =>
// object(Bar)[5]
// protected 'id' => int 1
// 1 =>
// object(Lol)[6]
// protected 'name' => null
// protected 'bar' =>
// object(Bar)[7]
// protected 'id' => int 2
// 2 =>
// object(Lol)[8]
// protected 'name' => null
// protected 'bar' =>
// object(Bar)[9]
// protected 'id' => int 3
// 3 =>
// object(Lol)[10]
// protected 'name' => null
// protected 'bar' =>
// object(Bar)[11]
// protected 'id' => int 4
$copyFoo = $newFoo;
$newID=10;
foreach($newFoo->getAllLols() as $lol){
$lol->getBar()->setID($newID++);
$lol->setName('bla');
}
var_dump($newFoo);
// object(Foo)[3]
// protected 'bar' => null
// protected 'lols' =>
// array (size=4)
// 0 =>
// object(Lol)[4]
// protected 'name' => string 'bla' (length=3)
// protected 'bar' =>
// object(Bar)[5]
// protected 'id' => int 10
// 1 =>
// object(Lol)[6]
// protected 'name' => string 'bla' (length=3)
// protected 'bar' =>
// object(Bar)[7]
// protected 'id' => int 11
// 2 =>
// object(Lol)[8]
// protected 'name' => string 'bla' (length=3)
// protected 'bar' =>
// object(Bar)[9]
// protected 'id' => int 12
// 3 =>
// object(Lol)[10]
// protected 'name' => string 'bla' (length=3)
// protected 'bar' =>
// object(Bar)[11]
// protected 'id' => int 13
var_dump($copyFoo);
// object(Foo)[3]
// protected 'bar' => null
// protected 'lols' =>
// array (size=4)
// 0 =>
// object(Lol)[4]
// protected 'name' => string 'bla' (length=3)
// protected 'bar' =>
// object(Bar)[5]
// protected 'id' => int 10
// 1 =>
// object(Lol)[6]
// protected 'name' => string 'bla' (length=3)
// protected 'bar' =>
// object(Bar)[7]
// protected 'id' => int 11
// 2 =>
// object(Lol)[8]
// protected 'name' => string 'bla' (length=3)
// protected 'bar' =>
// object(Bar)[9]
// protected 'id' => int 12
// 3 =>
// object(Lol)[10]
// protected 'name' => string 'bla' (length=3)
// protected 'bar' =>
// object(Bar)[11]
// protected 'id' => int 13
var_dump($newFoo===$copyFoo);
// boolean true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment