2012-03-15 15 views
0

私は学生オブジェクトを配列に保存したいと思います。私は以下のコードでしようとします。しかし、それは常に私は私が取得し、リストを見たいと思って、これまでとき静的リスト 中またはその後、結局生徒データを保存することが0を示して私はショーしようとすると、配列がカウント0Array Object in PHP

class Student 
{ 
    $StudID = 0; 
    $Name = null; 
} 
class Students 
{ 
    static private $StudentData = array(); 
    static public function AddNewStudent($id,$name) 
    { 
    echo("AuctionID :".$AuctionID."<br/>"); 
     try{ 
      $objstd = new Student(); 
      $objstd->StuID = $id; 
      $objstd->Name = &name; 
      array_push($StudentData, $objstd); 
     } 
     catch (Exception $e) 
     { 
      echo("Error".$e->getMessage()); 
     } 
    } 
    static public function TotalStudent() 
    { 
     return count($StudentData); 
    } 
} 


Students::AddNewStudent(1,"name"); 
Students::AddNewStudent(2,"name2"); 
Students::AddNewStudent(3,"name3"); 
echo('Total auction running : '.Students::TotalStudent().'<br/>'); 

として配列がカウントを示し静的クラスのみのリスト...

+0

あなたが実際にあなたは、おそらく代わりにグローバル変数を探している、ここでは静的な必要はありません。 – hakre

+0

あなたの助けは素晴らしいです。実際に私は静的なクラスを作ろうとしているので、ウェブサイトのすべての訪問者の間で同じデータを使用することができます。しかし、私はこの静的で私の目標を達成できない何が起こっているか知っている。しかし、今私はすべての訪問者の間で何か共通のデータをしたいので、同じことを達成するための考え方を教えてください。 – Ronak

+0

静的にしてください。静的ではありません。壊れるだろう。物事を難しくする。しばしば動作しません。悪いアイデア静的。あなたを傷つけるでしょう。 – hakre

答えて

4

宣言したものを参照するのではなく、新しい配列を作成するためです。あなたの静的オブジェクトのプロパティを参照するためにselfキーワードを使用します:PHPで

class Students 
{ 
    static private $StudentData = array(); 
    static public function AddNewStudent($id,$name) 
    { 
    echo("AuctionID :".$AuctionID."<br/>"); 
     try{ 
      $objstd = new Student(); 
      $objstd->StuID = $id; 
      $objstd->Name = &name; 
      array_push(self::$StudentData, $objstd); 
     } 
     catch (Exception $e) 
     { 
      echo("Error".$e->getMessage()); 
     } 
    } 
    static public function TotalStudent() 
    { 
     return count(self::$StudentData); 
    } 
} 
0

あなたはこのように、self::static variablesの前に付ける必要があります:それは複雑なのはなぜ

array_push(self::$StudentData, $objstd); 
// and in count: 

return count(self::$StudentData); 
0

?あなたのStudentクラスは、それ自身の世話をする必要があります。Studentsと同じです。例:

$students = new Students(); 
$students[] = new Student(1, "name"); 
$students[] = new Student(2, "name2"); 
$students[] = new Student(3, "name3"); 
printf('Total auction running : %d.', count($students)); 

出力例:

Total auction running : 3. 

クラス:

class Student 
{ 
    /** 
    * @var int 
    */ 
    private $id; 
    /** 
    * @var string 
    */ 
    private $name; 
    /** 
    * @param int $id 
    * @param string $name 
    */ 
    public function __construct($id, $name) { 
     $this->id = $id; 
     $this->name = $name; 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

} 

class Students extends ArrayObject 
{ 
    public function __construct() 
    { 
     parent::__construct(array()); 
    } 
    public function offsetSet($index, $newval) { 
     if (!($newval instanceof Student)) { 
      throw new InvalidArgumentException('You can only add values of type Student.'); 
     } 
     parent::offsetSet($index, $newval); 
    } 
}