通过添加/删除来管理项目集合

时间:2015-12-09 14:12:52

标签: php class oop

我希望有一个内存数据结构能够从我创建的集合类中添加或删除项目(在这个例子中是学生)到某种表(就像购物车)。目前,它只是显示学生。例如,如果我点击添加学生,它将在下面弹出,我也可以从下面删除这个学生。

我该如何实现?

这是我的Member.php类

<?php
 class Member {
  private $name;
  private $age;
  private $gender;
  private $course;

  public function __construct($name,$age, $gender, $course){
  $this->name = $name;
  $this->age = $age;
  $this->gender = $gender;
  $this->course = $course;
  }

  public function setName($name) { //Sets the age value

  $this->name = $name; 

  } 


 public function setAge($age) { //Sets the age value

 $this->age = $age; 

 } 



 public function setGender($gender) {  //Sets the gender value

 $this->gender = $gender; 

 } 

 public function setCourse ($course) {

    $this->course = $course;
 }

 public function getName() { //Gets the name value

 return $this->name; 

 } 

 public function getAge() {  //Gets the age value

 return $this->age; 

 } 


 public function getGender() { //Gets the gender value

 return $this->gender; 

 } 

 public function getCourse() {
     return $this->course;
 }
 }
 ?>

这是我的ObjectCollection.php

  <?php
 class ObjectCollection  
 {  
//This is an array to hold line items
  private $items_array ;

 private $itemCounter; //Count the number of items

 public function __construct() {
//Create an array object to hold line items
$this->items_array = array();
$this->itemCounter=0; 
 }

public function getItemCount(){
return $this->itemCounter;
}  

public function addItem($item) {
 $this->itemCounter++;
 $this->items_array[] = $item;
}

public function getItem($index) {
return $this->items_array[$index];
}
}
?>

最后通过testcollection.php显示这个

<?php
$ObjColl = new ObjectCollection();
$member1 = new Member("Jessica Davidson", 21, "Female", "Computing");
$ObjColl->addItem($member1);
$member2 = new Member("Lucy Barnes", 22, "Female", "History");
$ObjColl->addItem($member2);
$member3 = new Member("Mark Smith", 24, "Male", "Social Science");
$ObjColl->addItem($member3);

for($i = 0;$i < $ObjColl->getItemCount();$i++){
$item = $ObjColl->getItem($i);
if ($item instanceof Member) {
print "<br> University Member: ";
}

print "Name: " . $item->getName();
print ". Age: " . $item->getAge();
print ". Gender: " . $item->getGender();
print ". Enrolled on: " .$item->getCourse() . " course<br>";
}
?>

1 个答案:

答案 0 :(得分:0)

首先,如果您的ObjectCollection必须只收集Member类的对象,请使用参数类型声明。这是OOP的好习惯。

public function addItem(Member $item)

如果你想使用ObjectCollection和数组一样,那么在第二步,实现ArrayAccess和Iterator接口。

实施例

<?php
class Member{
    private $__name;

    public function __construct($name){
        $this->__name = $name;
    }

    public function getName(){
        return $this->__name;
    }
}

class MemberCollection implements ArrayAccess, Iterator{
    private $__Collection = [];

    private $__position = 0;

    public function __construct(){

    }

    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->__Collection[] = $value;
        } else {
            $this->__Collection[$offset] = $value;
        }
    }

    public function offsetExists($offset) {
        return isset($this->__Collection[$offset]);
    }

    public function offsetUnset($offset) {
        unset($this->__Collection[$offset]);
    }

    public function offsetGet($offset) {
        return isset($this->__Collection[$offset]) ? $this->__Collection[$offset] : null;
    }

    function rewind() {
        $this->__position = 0;
    }

    function current() {
        return $this->__Collection[$this->__position];
    }

    function key() {
        return $this->__position;
    }

    function next() {
        ++$this->__position;
    }

    function valid() {
        return isset($this->__Collection[$this->__position]);
    }

    public function addItem(Member $Member){
        $this->offsetSet(null, $Member);
    }
}

$MC = new MemberCollection();

$Member1 = new Member('Name 1');
$Member2 = new Member('Name 2');

$MC->addItem($Member1);
$MC->addItem($Member2);

foreach ($MC as $Member){
    echo '<br>' . $MC->key() . ':<br>';
    var_dump($Member->getName());
}

unset($MC[0]); //Delete member from collection
?>