Classes and Objects
Object State
An object can update its own state across method calls.
state changes
Method calls can update stored values, then return a scalar result that the page can display.
Object State
object_state.php
Replay: real traced execution (multi-file project)
<?php
class Counter
{
public int $value = 0;
public function add(int $amount): int
{
$this->value = $this->value + $amount;
return $this->value;
}
}
$step = 2;
$counter = new Counter();
$first = $counter->add($step);
$second = $counter->add(3);
echo "first=" . $first . "\n";
echo "second=" . $second . "\n";
<?php
class Counter
{
public int $value = 0;
public function add(int $amount): int
{
$this->value = $this->value + $amount;
return $this->value;
}
}
$step = 1;
$counter = new Counter();
$first = $counter->add($step);
$second = $counter->add(3);
echo "first=" . $first . "\n";
echo "second=" . $second . "\n";
<?php
class Counter
{
public int $value = 0;
public function add(int $amount): int
{
$this->value = $this->value + $amount;
return $this->value;
}
}
$step = 5;
$counter = new Counter();
$first = $counter->add($step);
$second = $counter->add(3);
echo "first=" . $first . "\n";
echo "second=" . $second . "\n";
$step ← 2, $counter ← Counter
13$step→ 2 = 2; //@step=1, 514$counter→ Counter = new Counter();15$first = $counterCounter->add($step2);16$second = $counter->add(3);$this->value ← 2
pass 1 of 26public function add(int $amount2): int7{8 $this->value→ 2 = $this->value + $amount2;9 return $this->value2;10}values this stepCounter$this$first ← 2
14$counter = new Counter();15$first→ 2 = $counterCounter->add($step2);16$second = $counterCounter->add(3);$this->value ← 5
pass 2 of 26public function add(int $amount3): int7{8 $this->value→ 5 = $this->value + $amount3;9 return $this->value5;10}values this stepCounter$this$second ← 5
15$first = $counter->add($step);16$second→ 5 = $counterCounter->add(3);1718echo "first=" . $first2 . "\n";19echo "second=" . $second5 . "\n";outputfirst=2 second=5
$step ← 1, $counter ← Counter
13$step→ 1 = 1;14$counter→ Counter = new Counter();15$first = $counterCounter->add($step1);16$second = $counter->add(3);$this->value ← 1
pass 1 of 26public function add(int $amount1): int7{8 $this->value→ 1 = $this->value + $amount1;9 return $this->value1;10}values this stepCounter$this$first ← 1
14$counter = new Counter();15$first→ 1 = $counterCounter->add($step1);16$second = $counterCounter->add(3);$this->value ← 4
pass 2 of 26public function add(int $amount3): int7{8 $this->value→ 4 = $this->value + $amount3;9 return $this->value4;10}values this stepCounter$this$second ← 4
15$first = $counter->add($step);16$second→ 4 = $counterCounter->add(3);1718echo "first=" . $first1 . "\n";19echo "second=" . $second4 . "\n";outputfirst=1 second=4
$step ← 5, $counter ← Counter
13$step→ 5 = 5;14$counter→ Counter = new Counter();15$first = $counterCounter->add($step5);16$second = $counter->add(3);$this->value ← 5
pass 1 of 26public function add(int $amount5): int7{8 $this->value→ 5 = $this->value + $amount5;9 return $this->value5;10}values this stepCounter$this$first ← 5
14$counter = new Counter();15$first→ 5 = $counterCounter->add($step5);16$second = $counterCounter->add(3);$this->value ← 8
pass 2 of 26public function add(int $amount3): int7{8 $this->value→ 8 = $this->value + $amount3;9 return $this->value8;10}values this stepCounter$this$second ← 8
15$first = $counter->add($step);16$second→ 8 = $counterCounter->add(3);1718echo "first=" . $first5 . "\n";19echo "second=" . $second8 . "\n";outputfirst=5 second=8