This may seem like an odd question. I do have some answers and I’m overall thinking this isn’t possible, but then I remember reading something about a type of variable that persists throughout an apps life. I’m not really sure how to word it so here is some PHP code that hopefully should communicate what I’m trying to do:
class Step
{
// This is the variable I want to auto increment somehow
public int $stepOrder = 1;
}
$steps = [
new Step(),
new Step(),
new Step(),
];
echo $steps[0]->stepOrder; // int 1
echo $steps[1]->stepOrder; // int 2
echo $steps[2]->stepOrder; // int 3
Hopefully you get the idea. Is this possible without using session or manually setting the order when instantiating?
>Solution :
Use a class static property that you increment in the constructor.
class Step
{
static int $currentStepOrder = 1;
public __construct() {
$this->stepOrder = self::$currentStepOrder++;
//...
}
}