2012-03-15 4 views
0

배열에 학생 개체를 저장하려고합니다. 그리고 나는 아래의 코드로하려고한다. 하지만 항상 내가 내가 얻을 목록을보고 싶은 그 어느 때 정적 목록 또는 다음 결국 학생 데이터를 저장할는 0을 보여줍니다 내가 표시 할 때 배열 카운트 0PHP에서 배열 개체

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 키워드를 사용

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

를 PHP에서이 같은 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); 
    } 
}