[英]class object in class with php oop
我什至不知道怎么问。 我一直在C#中执行此操作,但是对于我一生来说,我找不到在PHP中合适的示例。 你能帮我吗?
我想做的是例如。 假设我有一个叫做Company的班级,有很多员工。
我希望能够将所有员工存储在公司类中,然后在以后遍历Company对象时,我需要能够遍历分配给该公司的所有员工,并且我有一段时间尝试找到一个简单的例子。
我可以填充它,但是在我的一生中,我无法遍历员工。 我知道我做错了,但无法解决。 这是我的例子。
雇员
对于我所知,取回数据,这可能是完全错误的。
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...
有人可以提供一些指导吗?
谢谢
版本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";
}
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;
}
因为没有初始化程序的first
和last
字段被初始化为null
。 isset
,在您的情况下,仅检查参数是否为null,因为可以保证设置了该参数(这是一个参数)。
PHP在该语言中内置了称为PHP标准库的东西。 PHP标准库带有许多接口,使您可以将某些核心语言功能实现到自己定义的对象中。 这些都是迭代器接口。 定义一个实现此接口的类,然后编写一个实现,当您的对象放入foreach循环中时,该实现将允许您的对象执行您想要的操作。
另外,根据您所追求的语义,您可以轻松地执行
foreach($company->getEmployees() as $employee)
{
var_dump($employee);
}
getEmployees
方法将仅被调用一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.