note 75412 added to function.ArrayObject-offsetUnset
| From: | oalexandrino at yahoo dot com dot br | Date: | Mon, 28 May 2007 18:11:11 +0000 |
| Subject: | note 75412 added to function.ArrayObject-offsetUnset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126985@lists.php.net to get a copy of this message | ||
Be careful when you are working with collections. This method works with the reference of an array
instead of its retrieved value.
So, you can do a mistake.
In order to understand have a look at code as follow:
<?php
class Employee
{
public function __construct()
{
}
}
class Company
{
private $arrEmployee;
public function __construct()
{
}
public function AddEmployee(Employee $oEmployee)
{
$this->arrEmployee[] = $oEmployee;
}
public function getEmployeeList()
{
return $this->arrEmployee;
}
}
?>
<?php
// first, creates the Company object
$oCompany = new Company();
// second, add 10 elements in
foreach( range(0, 9) as $index )
{
$oCompany->AddEmployee( new Employee() );
}
// get them
$arrEmployee = $oCompany->getEmployeeList();
// creates an ArrayObject from "$arrEmployee"
$arrayobject = new ArrayObject($arrEmployee);
// unsets its firt five elements
foreach( range(0, 4) as $index )
{
$arrayobject->offsetUnset($index);
}
// get them again
$arrEmployee = $oCompany->getEmployeeList();
// it shows just 5 elements, they were removed as reference via "offsetUnset" method
print_r($arrEmployee) ;
?>
----
Server IP: 200.185.126.110
Probable Submitter: 200.199.23.121
----
Manual Page -- http://www.php.net/manual/en/function.ArrayObject-offsetUnset.php
Edit -- https://master.php.net/note/edit/75412
Del: integrated -- https://master.php.net/note/delete/75412/integrated
Del: useless -- https://master.php.net/note/delete/75412/useless
Del: bad code -- https://master.php.net/note/delete/75412/bad+code
Del: spam -- https://master.php.net/note/delete/75412/spam
Del: non-english -- https://master.php.net/note/delete/75412/non-english
Del: in docs -- https://master.php.net/note/delete/75412/in+docs
Del: other reasons-- https://master.php.net/note/delete/75412
Reject -- https://master.php.net/note/reject/75412
Search -- https://master.php.net/manage/user-notes.php