title: class object in class with php oop
body: <p>I am not even sure how to ask.  I do this all the time in C# but for the life of me I cannot find any decent examples in PHP.  Can you help?</p>
  
  <p>What I am trying to do is for example.  Say I have a class called Company who has many Employees.</p>
  
  <p>I want to be able to store all the employees in the company class then later when I iterate through the Company object I need to be able to iterate through all the employees that are assigned to that company and I am having a heck of a time trying to find a straight forward example.</p>
  
  <p>I can populate it, but fro the life of me I cant loop through the Employees.  I know I am doing something wrong but cannot figure it out.  Here is what I have for example.</p>
  
  <p>Employee </p>
  
  <p>This could be completely wrong for all I know as getting the data back out.</p>
  
  <pre><code>    class Employee
      {
          private $first;
          private $last;
  
          public function __construct($first = null, $last = null)
          {
              if(isset ($first))
                  $this-&gt;first = $first;
  
              if(isset ($last))
                  $this-&gt;last = $last;
          }
  
          public function getEmployee()
          {
              return Employee($this-&gt;first, $this-&gt;last);
          }
  
          public function getFirst()
          {
              return $this-&gt;first;
          }
  
          public function getLast()
          {
              return $this-&gt;first;
          }
      }
  </code></pre>
  
  <p>Company has an array of employees.  </p>
  
  <p>In c# I would use something like a generic </p>
  
  <pre><code>    public List&lt;Employee&gt; Employees {get;set;}
  </code></pre>
  
  <p>Then in the constructor I would do something like </p>
  
  <pre><code>    Employees = new List&lt;Employee&gt;();
  </code></pre>
  
  <p>Then I could do something like</p>
  
  <pre><code>    foreach(var x in Customer.Employees)
              x.first;
  </code></pre>
  
  <p>That is what I am kind of trying to do.</p>
  
  <pre><code>    class Company
      {
          private $companyName;
  
          private $employees;
  
  
          public function __construct($nameOfCompany)
          {
              $this-&gt;companyName = $nameOfCompany;               
          }
  
          public function setEmployee(Employee $employee)
          {
              $this-&gt;employees[] = $employee;
          }
  
          public function getCompanyName()
          {
              return $this-&gt;companyName;
          }
  
          public function setCompanyName($nameOfCompany)
          {
              $this-&gt;companyName = $nameOfCompany;
          }
  
          public function getEmployees()
          {
              return $this-&gt;employees;
          }
      }
  </code></pre>
  
  <p>Create and populate</p>
  
  <pre><code>    $c = new Company("Test");
      $c-&gt;setEmployee(new Employee('A','B'));
      $c-&gt;setEmployee(new Employee('C','D'));
      $c-&gt;setEmployee(new Employee('E','F'));
  </code></pre>
  
  <p>Up until this point all is well.</p>
  
  <p>To get the name of the company no problem.</p>
  
  <pre><code>    echo $c-&gt;getCompanyName();
  
      echo "&lt;PRE&gt;";
      print_r($c);
      echo "&lt;/PRE&gt;";
  </code></pre>
  
  <p>But how do I loop through the employees?</p>
  
  <p>I want to be able to do something like cast Companies employees in a loop to a single Employee then do something like </p>
  
  <pre><code>    foreach(customers employees)
             echo Employee-&gt;getFirst, etc...
  </code></pre>
  
  <p>Can someone please provide some guidance?  </p>
  
  <p>Thanks</p>