dongshi2458 2013-11-15 18:45
浏览 35
已采纳

如何让Factory Class在PHP中创建对象实例?

I have an XML file with many member entries, formatted like so:

<staff>
    <member>
        <name></name>
        <image></image>
        <title></title>
        <email></email>
        <phone></phone>
        <location></location>
        <info></info>
        <webTitle></webTitle>
        <webURL></webURL>
    </member>
</staff>

setup

I've created 2 PHP classes, DisplayStaff and Employee.

  • Employee creates an Employee object, with private properties outlined in the XML above.
  • DisplayStaff is a factory class. It loads the above XML file, iterates through it, creating an Employee instance for each <member> element in the XML. It stores the Employee object in an array, $Employees[].

On the page where I want to output the Employee information, I'd like to be able to reference it like so.

$Employees = new DisplayStaff();
$John = Employees['John'];
echo "Hello, my name is $John->name";

code

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

<?php
class Employee
{

     private var $name     = "";
     private var $image    = "";
     private var $title    = "";
     private var $email    = "";
     private var $phone    = "";
     private var $location = "";
     private var $info     = "";
     private var $webTitle = "";
     private var $webURL   = "";

        public function get($propertyName) {
        if( property_exists($this, $propertyName) ) {
            return $this->$propertyName;
        }
            else{
                return null;
           }
    }

    public function set($propertyName, $propertyValue) {
        if( property_exists($this, $propertyName) ){
            $this->$propertyName = $propertyValue;
        }
    }
}

?>

problem

I can't seem to get this working. I'm new to working with classes. What do I need to change to have my classes behave how I desire them to?

Thank you in advanced for any help.

  • 写回答

2条回答 默认 最新

  • doq1969 2013-11-15 20:20
    关注
    1. Remove var from each of your class fields. That is redundant with public and also deprecated as of PHP5. More on this

    2. The constructor should not return anything. It will automatically return the object in question. Note, however, that there is no way of accessing any of the DisplayStaff fields since they are all private with no accessor functions. You could use the universal accessors like you do in Employee, but is there a reason for not simply using public fields?

    3. Any time you are referring to an method or property of an object within the class declaration, you need to use the $this keyword, i.e.

      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
      }
      
    4. Your Employee property accessor method (Employee->get()) takes a string as the first parameter, i.e. 'name'. $name is an undefined variable so it won't work.

    5. You need to initialize your Employees array before you can push to it.

      private $Employees = array();
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。