Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:32:49+00:00 2026-05-16T01:32:49+00:00

I am not even sure how to ask. I do this all the time

  • 0

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?

What I am trying to do is for example. Say I have a class called Company who has many Employees.

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.

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.

Employee

This could be completely wrong for all I know as getting the data back out.

    class Employee
    {
        private $first;
        private $last;

        public function __construct($first = null, $last = null)
        {
            if(isset ($first))
                $this->first = $first;

            if(isset ($last))
                $this->last = $last;
        }

        public function getEmployee()
        {
            return Employee($this->first, $this->last);
        }

        public function getFirst()
        {
            return $this->first;
        }

        public function getLast()
        {
            return $this->first;
        }
    }

Company has an array of employees.

In c# I would use something like a generic

    public List<Employee> Employees {get;set;}

Then in the constructor I would do something like

    Employees = new List<Employee>();

Then I could do something like

    foreach(var x in Customer.Employees)
            x.first;

That is what I am kind of trying to do.

    class Company
    {
        private $companyName;

        private $employees;


        public function __construct($nameOfCompany)
        {
            $this->companyName = $nameOfCompany;               
        }

        public function setEmployee(Employee $employee)
        {
            $this->employees[] = $employee;
        }

        public function getCompanyName()
        {
            return $this->companyName;
        }

        public function setCompanyName($nameOfCompany)
        {
            $this->companyName = $nameOfCompany;
        }

        public function getEmployees()
        {
            return $this->employees;
        }
    }

Create and populate

    $c = new Company("Test");
    $c->setEmployee(new Employee('A','B'));
    $c->setEmployee(new Employee('C','D'));
    $c->setEmployee(new Employee('E','F'));

Up until this point all is well.

To get the name of the company no problem.

    echo $c->getCompanyName();

    echo "<PRE>";
    print_r($c);
    echo "</PRE>";

But how do I loop through the employees?

I want to be able to do something like cast Companies employees in a loop to a single Employee then do something like

    foreach(customers employees)
           echo Employee->getFirst, etc...

Can someone please provide some guidance?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:32:50+00:00Added an answer on May 16, 2026 at 1:32 am

    version 1: explicit getter method

    <?php
    class Employee
    {
      private $first;
      private $last;
    
      public function __construct($first = null, $last = null)
      {
        if(isset ($first))
        $this->first = $first;
    
        if(isset ($last))
        $this->last = $last;
      }
    
      public function getFirst()
      {
        return $this->first;
      }
    
      public function getLast()
      {
        return $this->first;
      }
    }
    
    class Company
    {
      private $companyName;
      private $employees;
    
      public function __construct($nameOfCompany)
      {
        $this->companyName = $nameOfCompany;
      }
    
      public function addEmployee(Employee $employee)
      {
        $this->employees[] = $employee;
      }
    
      public function getCompanyName()
      {
        return $this->companyName;
      }
    
      public function getEmployees()
      {
        return $this->employees;
      }
    }
    
    $company = new Company('ACME');
    $company->addEmployee(new Employee('A', 'A'));
    $company->addEmployee(new Employee('B', 'B'));
    $company->addEmployee(new Employee('C', 'C'));
    
    foreach( $company->getEmployees() as $e ) {
      echo $e->getFirst(), "\n";
    }
    

    version 2: using __get

    class Company
    {
      private $companyName;
      private $employees;
    
      public function __construct($nameOfCompany)
      {
        $this->companyName = $nameOfCompany;
      }
    
      public function addEmployee(Employee $employee)
      {
        $this->employees[] = $employee;
      }
    
      public function __get($name) {
         // this will allow access to any member of the class
         // so you might want to test access first
         if ( isset($this->$name) ) {
           return $this->$name;
         }
       }
    
    }
    
    $company = new Company('ACME');
    $company->addEmployee(new Employee('A', 'A'));
    $company->addEmployee(new Employee('B', 'B'));
    $company->addEmployee(new Employee('C', 'C'));
    
    foreach( $company->employees as $e ) {
      echo $e->getFirst(), "\n";
    }
    
    $company = new Company('ACME');
    $company->addEmployee(new Employee('A', 'A'));
    $company->addEmployee(new Employee('B', 'B'));
    $company->addEmployee(new Employee('C', 'C'));
    
    foreach( $company->employees as $e ) {
      echo $e->getFirst(), "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if this is even possible but here goes: Currently I have
I'm not even sure if it's possible but say I have some XML: <source>
I'm not sure if this even exists or not, so I figured I would
I work on Linux all the time and I'm clueless about Windows, not even
currently, I am looking deeper into testing techniques, even though I am not sure
I want to write Html format, but I can not even get a simple
OK, this is an odd request, and it might not even be fully true...
In how many languages is Null not equal to anything not even Null?
I can't seem to get it to work. Perhaps I'm not even testing it
I would like to stop images from loading, as in not even get a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.