Ronak
Ronak

Reputation: 19

Array Object In Php

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...

Upvotes: 0

Views: 790

Answers (3)

hakre
hakre

Reputation: 197682

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

Upvotes: 0

Vyktor
Vyktor

Reputation: 20997

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

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

return count(self::$StudentData);

Upvotes: 0

Cfreak
Cfreak

Reputation: 19309

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

Upvotes: 4

Related Questions