简体   繁体   English

使用php oop的类中的class对象

[英]class object in class with php oop

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. 我一直在C#中执行此操作,但是对于我一生来说,我找不到在PHP中合适的示例。 Can you help? 你能帮我吗?

What I am trying to do is for example. 我想做的是例如。 Say I have a class called Company who has many Employees. 假设我有一个叫做Company的班级,有很多员工。

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. 我希望能够将所有员工存储在公司类中,然后在以后遍历Company对象时,我需要能够遍历分配给该公司的所有员工,并且我有一段时间尝试找到一个简单的例子。

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 在C#中,我会使用类似泛型的东西

    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 谢谢

version 1: explicit getter method 版本1:显式getter方法

<?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 版本2:使用__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";
}
foreach ($company->getEmployees() as $emp) {
    //do something with $emp
}

See foreach . 参见foreach

One note: 一注:

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

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

is exactly the same as 与...完全相同

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

because the fields first and last , having no initializer, are initialized to null . 因为没有初始化程序的firstlast字段被初始化为null isset , in your case, only checks if the argument's null, since it's guaranteed to be set (it's an argument). isset ,在您的情况下,仅检查参数是否为null,因为可以保证设置了该参数(这是一个参数)。

PHP has something called the PHP Standard Library built into the language. PHP在该语言中内置了称为PHP标准库的东西。 The PHP Standard Library comes with a number of interfaces that allow you to implement certain core language functionality into your own defined objects. PHP标准库带有许多接口,使您可以将某些核心语言功能实现到自己定义的对象中。 Once of these is the iterator interface. 这些都是迭代器接口。 Define a class that implements this interface, and then write an implementation that will allow your object to do what you want when it's placed in a foreach loop. 定义一个实现此接口的类,然后编写一个实现,当您的对象放入foreach循环中时,该实现将允许您的对象执行您想要的操作。

Also, depending on the semantic you're after, you could just as easily do a 另外,根据您所追求的语义,您可以轻松地执行

foreach($company->getEmployees() as $employee)
{
    var_dump($employee);
}

The getEmployees method will only be called once. getEmployees方法将仅被调用一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM