0

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.

tdc
  • 5,174
  • 12
  • 53
  • 102
  • DisplayStaff sounds like it should have a different name. Also, it is hardly a factory, since it mainly parses an XML file and translates it's contents to an object array. About your question: did you get any errors? If not, have your tried stepping through your code or dumping variables to see what is going on? – GolezTrol Nov 15 '13 at 19:47

2 Answers2

1
  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();
    
Community
  • 1
  • 1
Sean Fraser
  • 342
  • 3
  • 14
0

Note that I left out many of the Employee properties and methods for the sake of brevity.

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>';
}
Dave
  • 3,658
  • 1
  • 16
  • 9