0

i want to store Student Object to array. and i try to do with below code. but it always show array count as 0

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/>');

when i try to show array count it shows 0. i want to store all student data in static list or then after when ever i want to see the list i get the list from static class only...

3
  • You actually don't need static here, you're probably looking for a global variable instead. Commented Mar 15, 2012 at 19:53
  • Thanx buddy your help is great. but actually i am trying to make a static class so i can use the same data between all visitors of website. but i cant achieve my goal with this static dont know what going on. But now i want to do something common data between all the visitors so plz give me some idea how to achieve the same. Commented Mar 16, 2012 at 11:11
  • Forget static. Static is not good. Will break. Make things hard. Often does not work. Bad idea static. Will make you hurt. Commented Mar 16, 2012 at 11:26

3 Answers 3

4

Because you're creating a new array instead of referencing the one you declared. Use the self keyword to reference your static object property:

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In php you have to prefix static variables with self::, like this:

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

return count(self::$StudentData);

Comments

0

Why that complicated? Your Student class should take care of it's own, same for Students. Example:

$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));

Example output:

Total auction running : 3.

The classes:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.