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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

last question can i use 2 constructors? one with variables and one without function __construct($arg) { this->id = $arg; } ... thanks
A great answer for that is here: stackoverflow.com/questions/1699796/…
You could also default the variable passed in like: __construct($arg = null){ if(isset($arg)) $this->id = $arg;
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();
...

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.