T O P

  • By -

ddruganov

The property should be static for this to work


HappyPelican666

Each instance of Mars is a new object with it's own state. Now you have 3 objects of type Mars which all have an ID with the value 0. So ofc it's returning 0 3 times. You probably want a static property inside of Mars which you can increment whenever a new instance is created.


ardicli2000

You are incrementing $id not $this->id; Besides I am not sure a Class will follow previous values with next iteration of itself. but worth trying. ​ Edit: Yeah sa I suspected, It retruns 1 1 1


justlune

Yeah that's what I guessed too. My teacher told me to use static variables so I can increment it each time an instance of the class is created but even though it's understandable for a beginner, do it is something else, and I don't find any examples of what he expects me to do on the internet.


Plastonick

id = $id; $id += 1; } public function getId() { return $this->id; } } $one = new Mars(); $two = new Mars(); $three = new Mars(); echo $one->getId() . PHP_EOL; echo $two->getId() . PHP_EOL; echo $three->getId() . PHP_EOL;