如何让Factory类在PHP中创建对象实例?
我有一个包含许多成员条目的XML文件,格式如下:如何让Factory类在PHP中创建对象实例?,php,xml,factory-pattern,Php,Xml,Factory Pattern,我有一个包含许多成员条目的XML文件,格式如下: <staff> <member> <name></name> <image></image> <title></title> <email></email> <phone></phone> <lo
<staff>
<member>
<name></name>
<image></image>
<title></title>
<email></email>
<phone></phone>
<location></location>
<info></info>
<webTitle></webTitle>
<webURL></webURL>
</member>
</staff>
代码
<?php
class DisplayStaff
{
private var $staffList;
private var $placeholderImg;
private var $Employees;
public function __construct() {
$staffList = simplexml_load_file("../data/staff.xml");
$placeholderImg = $staffList->placeholderImg;
foreach ( $staffList->member as $member ) {
$employee = formatEmployeeObj( $member );
array_push( $Employees, $employee );
}
return $Employees;
}
private function formatEmployeeObj( $memberXML ) {
$Employee = new Employee();
$Employee->set( $name, $memberXML->name );
$Employee->set( $image, $memberXML->image );
$Employee->set( $title, $memberXML->title );
$Employee->set( $email, $memberXML->email );
$Employee->set( $phone, $memberXML->phone );
$Employee->set( $location, $memberXML->location );
$Employee->set( $info, $memberXML->info );
$Employee->set( $webTitle, $memberXML->webTitle );
$Employee->set( $webURL, $memberXML->webURL );
return $Employee;
}
}
?>
问题
我好像没法让它工作。我刚开始上课。我需要做什么改变才能让我的班级按照我所希望的方式运作
感谢您在advanced中提供的帮助。注意,为了简洁起见,我省略了许多Employee属性和方法
class StaffFactory
{
public $employees = array();
public function __construct($xml)
{
$xml = simplexml_load_file($xml);
foreach ( $xml->member as $member ) {
$this->employees[(string) $member->name] = new Employee((array) $member);
}
}
public function getEmployee($name)
{
if ( isset($this->employees[$name]) ) {
return $this->employees[$name];
}
return false;
}
}
class Employee {
public $email;
public $name;
public $title;
public function __construct(array $employee)
{
foreach ( $employee as $k => $v ) {
if ( property_exists($this, $k) ) {
$this->{$k} = $v;
}
}
return $this;
}
}
$staffFactory = new StaffFactory('staff.xml');
$john = $staffFactory->getEmployee('John');
if ( $john ) {
echo '<pre>';
print_r($staffFactory);
print_r($john);
print_r($john->email);
echo '</pre>';
}
class员工工厂
{
public$employees=array();
公共函数构造($xml)
{
$xml=simplexml\u加载文件($xml);
foreach($xml->member as$member){
$this->employees[(字符串)$member->name]=新员工((数组)$member);
}
}
公共函数getEmployee($name)
{
如果(设置($this->employees[$name])){
返回$this->employees[$name];
}
返回false;
}
}
班级员工{
公费邮件;
公共名称;
公有产权;
公共函数构造(数组$employee)
{
foreach($k=>v的员工){
如果(财产_存在($this,$k)){
$this->{$k}=$v;
}
}
退还$this;
}
}
$staffFactory=newstafffactory('staff.xml');
$john=$staffFactory->getEmployee('john');
如果(约翰){
回声';
印刷厂(staffFactory);
印刷(约翰);
打印(约翰->电子邮件);
回声';
}
var
。这与public
是多余的,并且从PHP5开始就被弃用Employee
中那样使用通用访问器,但有没有理由不简单地使用public
字段$this
关键字,即
private $Employees = array();
Employee->get()
)将字符串作为第一个参数,即'name'
$name
是一个未定义的变量,因此无法工作DisplayStaff听起来应该有不同的名称。此外,它也不是工厂,因为它主要解析XML文件并将其内容转换为对象数组。关于你的问题:你有错误吗?如果没有,您是否尝试过单步执行代码或转储变量以查看发生了什么?
class StaffFactory
{
public $employees = array();
public function __construct($xml)
{
$xml = simplexml_load_file($xml);
foreach ( $xml->member as $member ) {
$this->employees[(string) $member->name] = new Employee((array) $member);
}
}
public function getEmployee($name)
{
if ( isset($this->employees[$name]) ) {
return $this->employees[$name];
}
return false;
}
}
class Employee {
public $email;
public $name;
public $title;
public function __construct(array $employee)
{
foreach ( $employee as $k => $v ) {
if ( property_exists($this, $k) ) {
$this->{$k} = $v;
}
}
return $this;
}
}
$staffFactory = new StaffFactory('staff.xml');
$john = $staffFactory->getEmployee('John');
if ( $john ) {
echo '<pre>';
print_r($staffFactory);
print_r($john);
print_r($john->email);
echo '</pre>';
}
public function __construct() {
$this->staffList = simplexml_load_file("staff.xml");
$this->placeholderImg = $this->staffList->placeholderImg
foreach ( $this->staffList->member as $member ) {
$employee = $this->formatEmployeeObj( $member );
array_push( $this->Employees, $employee );
}
//no need for a return
}
private $Employees = array();