1

I would need a class like:

<?php
class  Replay{

    private $id;
    private $nickname;


    public function setId($id_){
       $this->id = $id_;
    }
    public function setNickname($nickname_){
       $this->nickname = $nickname_;
    }

    public function getId(){
       return $this->id;
    }
    public function getNickname(){
       return $this->nickname;
    }
}
?>

and than I would make another class replays that would hold an array of replay. (repository)

I don`t know how to declare the array of replay, if anyone has some examples? even more complex with sorting and other functions if now only the basic stuff.

<?php
class  Replays{

    private $number;  //number of elements
    ...
?>
Ryan
  • 26,884
  • 9
  • 56
  • 83
Wisp Ever
  • 35
  • 6

2 Answers2

2

You would just create an array and add to them as needed:

<?php
class Replays {
    private $replays = array();

    public function addReplay($replay){
        replays[] = $replay;
    }

    public function getNumReplays(){
        return count($replays);
    }

}
?>

Not sure if you are used to java, but arrays in php do not need to know the type they are holding. For example and array can hold strings and integers at the same time:

<?php
$array = array("string", 2, 2.0);
var_dump($array);

?>

Output:

array(3) {
  [0] =>
  string(6) "string"
  [1] =>
  int(2)
  [2] =>
  double(2)
}

As you can see PHP understands the types and they can all be in the same array.

immulatin
  • 2,118
  • 1
  • 12
  • 13
  • last question can i use 2 constructors? one with variables and one without function __construct($arg) { this->id = $arg; } ... thanks – Wisp Ever Jul 18 '13 at 16:28
  • A great answer for that is here: http://stackoverflow.com/questions/1699796/best-way-to-do-multiple-constructors-in-php – immulatin Jul 18 '13 at 16:38
  • You could also default the variable passed in like: `__construct($arg = null){ if(isset($arg)) $this->id = $arg;` – immulatin Jul 18 '13 at 16:39
1

In PHP arrays do not carry a particular type. So you can simply declare an array as

$repo = array ();

and then add multiple entries to it

$repo[] = new Replay();
$repo[] = new Replay();
...
Ryan
  • 26,884
  • 9
  • 56
  • 83