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

Im not even sure how to ask this question, but i'll give it a
I'm sad to have to ask this question, but I'm not even sure of
I am not even sure how to ask this question. I want something that
I'm not really sure how to even ask the question, let alone find an
Not even sure how to explain this but I connect to a remote computer
I'm not even sure what this is called? But I'm trying to learn what
I'm not sure if this is the correct place to ask, but as it's
I'm not even 100% sure how to ask this question. The answer might be
I'm not even really sure how to ask this. The LESS CSS Framework contains
I'm not even sure how I should phrase this question. I'm passing some CustomStruct

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.