# PHP Objects and cyclical references - HELP

- Thread: https://externals.io/message/1410
- Original archive: https://news-web.php.net/php.internals/1410
- Started by Paul Lunceford on 2003-05-09T14:59:55+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #1410 — Paul Lunceford — 2003-05-09T14:59:55+00:00

URL: https://externals.io/message/1410#1410

I am just getting to know PHP and am a big proponent of object structures.
I am trying to use PHP classes that get instanciated on the fly from data
within MySQL.  My problem is this.  If I have a Department class that has as
a variable Employees and an Employee class (along with an Employees class
with a list variable for getting sets of employees) that has a variable
Division (to navigate back and forth).  I seem to have problems getting the
objects to point to one another.  PHP does not seem to like cyclical
references and makes copies of my objects.  So if I have the following
function in my Division class:

function getEmployees()
{
  $myEmployees = new Employees($this->DivisionID);
   forEach ($myEmployees->list as $employee)
   {
      $employee->Division = $this;
      $this->Employees[$employee->EmployeeNumber] = $employee;
   }
}

the reference of $employee->Division becomes only a copy of the $this
object.  This is a problem because the first employee to go through the loop
when asked other employees within his division sees none, thinking himself
the only employee of the division.  Does anyone know the solution?  Please
email response to paul.luceford@enterworks.com.  Thanks.

---

## #1413 — Moriyoshi Koizumi — 2003-05-10T04:03:57+00:00

URL: https://externals.io/message/1410#1413 · In reply to: #1410

Hi,

This list is intended for the development of php runtime, not for such 
user questions. Please ask your questions at php-general@lists.php.net.

Thanks.

Moriyoshi

hint: use "&" 

"Paul Lunceford" <luncep@comcast.net> wrote:

> I am just getting to know PHP and am a big proponent of object structures.
> I am trying to use PHP classes that get instanciated on the fly from data
> within MySQL.  My problem is this.  If I have a Department class that has as
> a variable Employees and an Employee class (along with an Employees class
> with a list variable for getting sets of employees) that has a variable
> Division (to navigate back and forth).  I seem to have problems getting the
> objects to point to one another.  PHP does not seem to like cyclical
> references and makes copies of my objects.  So if I have the following
> function in my Division class:
> 
> function getEmployees()
> {
>   $myEmployees = new Employees($this->DivisionID);
>    forEach ($myEmployees->list as $employee)
>    {
>       $employee->Division = $this;
>       $this->Employees[$employee->EmployeeNumber] = $employee;
>    }
> }
> 
> the reference of $employee->Division becomes only a copy of the $this
> object.  This is a problem because the first employee to go through the loop
> when asked other employees within his division sees none, thinking himself
> the only employee of the division.  Does anyone know the solution?  Please
> email response to paul.luceford@enterworks.com.  Thanks.
