2010-08-02 142 views
1

我甚至不知道如何问。我在C#中一直这样做,但在我的生活中,我无法在PHP中找到任何体面的示例。你能帮我吗?类中的类对象与PHP OOP

我想要做的是例如。假设我有一个叫公司的班级,他有很多员工。

我希望能够将所有员工存储在公司类中,然后当我迭代公司对象时,我需要能够遍历分配给该公司的所有员工,并且我正在摸索一段时间试图找到一个直接的例子。

我可以填充它,但为了我的生活我无法循环通过员工。我知道我做错了什么,但无法弄清楚。这是我的例子。

员工

这可能是完全错误的,我才知道作为获取数据退了出去。

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; 
     } 
    } 

公司拥有一批员工。

在C#中我会使用类似的通用

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

然后在构造函数中我会做类似

Employees = new List<Employee>(); 

然后,我可以做类似

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

那是我有点想做的事情。

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; 
     } 
    } 

创建并填充

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

直到此时一切都很好。

要获得公司的名称没有问题。

echo $c->getCompanyName(); 

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

但是,我如何循环通过员工?

我希望能够像做投公司的员工在一个循环中的单个雇员,然后像做

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

是否有人可以提供一些指导?

感谢

回答

3

版本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"; 
} 

版本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"; 
} 
+0

不要用'__get'给他提出想法 - 至少不是没有告诉他关于固有问题。 – Artefacto 2010-08-02 00:20:45

+0

非常感谢您的回复。它让我朝着正确的方向前进。 问题,是否因为__get允许你这样做, foreach($ company-> employees as $ e)? 我想说是的,但我只是想确保我理解它。还有什么是陷阱? Artefacto提到了固有的问题? 再次感谢您 – nitefrog 2010-08-02 02:54:57

+0

@nite __get比直接访问对象属性慢得多(在下一版本的PHP中更是如此)。在一个沉重的面向对象的应用程序中,它可能会有所作为。 – Artefacto 2010-08-02 07:03:11

2
foreach ($company->getEmployees() as $emp) { 
    //do something with $emp 
} 

foreach

一个注意:

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

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

是完全一样的

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

因为没有初始化的字段firstlast,被初始化为nullisset,在你的情况下,只检查参数是否为null,因为它保证被设置(它是一个参数)。

+0

谢谢你的答案。我想我试图做一个强大的$ emp类型。 例如像(Employee)$ emp,或者在PHP中不可能实现? 再次非常感谢你的快速回复。 – nitefrog 2010-08-02 02:46:20

+0

@nite如果这就是你要求的,这些转换在PHP中是不可能的。 – Artefacto 2010-08-02 03:16:53

+0

好的,现在它是有道理的...... – nitefrog 2010-08-02 03:34:00

2

PHP有一种叫做PHP标准库内置的语言。 PHP标准库带有许多接口,允许您将某些核心语言功能实现到您自己定义的对象中。一旦这些是iterator接口。定义一个实现此接口的类,然后编写一个实现,该实现将允许您的对象在放入foreach循环时执行所需操作。

此外,根据语义你之后,你可以很容易地做一个

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

getEmployees方法才会被调用一次。

+0

你好Alan,谢谢你的安息。我不知道迭代器。从我完成任何PHP和来自其他语言的人到现在已经很长时间了,这有点令人困惑。 再次谢谢你... – nitefrog 2010-08-02 02:47:45