A method is a function that belongs to a class.

object behavior A method can combine object behavior with changing input from the main script.

Method Calls

tax
method_calls.php
Replay: real traced execution (multi-file project)
<?php
class Calculator
{
    public function addTax(int $subtotal, int $tax): int
    {
        return $subtotal + $tax;
    }
}

$tax = 5;
$subtotal = 40;
$total = (new Calculator())->addTax($subtotal, $tax);

echo "subtotal=" . $subtotal . "\n";
echo "tax=" . $tax . "\n";
echo "total=" . $total . "\n";
<?php
class Calculator
{
    public function addTax(int $subtotal, int $tax): int
    {
        return $subtotal + $tax;
    }
}

$tax = 0;
$subtotal = 40;
$total = (new Calculator())->addTax($subtotal, $tax);

echo "subtotal=" . $subtotal . "\n";
echo "tax=" . $tax . "\n";
echo "total=" . $total . "\n";
<?php
class Calculator
{
    public function addTax(int $subtotal, int $tax): int
    {
        return $subtotal + $tax;
    }
}

$tax = 12;
$subtotal = 40;
$total = (new Calculator())->addTax($subtotal, $tax);

echo "subtotal=" . $subtotal . "\n";
echo "tax=" . $tax . "\n";
echo "total=" . $total . "\n";
  1. $tax ← 5, $subtotal ← 40

    10$tax→ 5 = 5; //@tax=0, 1211$subtotal→ 40 = 40;12$total = (new Calculator())->addTax($subtotal40, $tax5);
  2. public function addTax(int $subtotal, int $tax): int

    3{4    public function addTax(int $subtotal40, int $tax5): int5    {6        return $subtotal40 + $tax5;7    }
  3. $total ← 45

    11$subtotal = 40;12$total→ 45 = (new Calculator())->addTax($subtotal40, $tax5);1314echo "subtotal=" . $subtotal40 . "\n";15echo "tax=" . $tax5 . "\n";16echo "total=" . $total45 . "\n";
    outputsubtotal=40
    tax=5
    total=45
  1. $tax ← 0, $subtotal ← 40

    10$tax→ 0 = 0;11$subtotal→ 40 = 40;12$total = (new Calculator())->addTax($subtotal40, $tax0);
  2. public function addTax(int $subtotal, int $tax): int

    3{4    public function addTax(int $subtotal40, int $tax0): int5    {6        return $subtotal40 + $tax0;7    }
  3. $total ← 40

    11$subtotal = 40;12$total→ 40 = (new Calculator())->addTax($subtotal40, $tax0);1314echo "subtotal=" . $subtotal40 . "\n";15echo "tax=" . $tax0 . "\n";16echo "total=" . $total40 . "\n";
    outputsubtotal=40
    tax=0
    total=40
  1. $tax ← 12, $subtotal ← 40

    10$tax→ 12 = 12;11$subtotal→ 40 = 40;12$total = (new Calculator())->addTax($subtotal40, $tax12);
  2. public function addTax(int $subtotal, int $tax): int

    3{4    public function addTax(int $subtotal40, int $tax12): int5    {6        return $subtotal40 + $tax12;7    }
  3. $total ← 52

    11$subtotal = 40;12$total→ 52 = (new Calculator())->addTax($subtotal40, $tax12);1314echo "subtotal=" . $subtotal40 . "\n";15echo "tax=" . $tax12 . "\n";16echo "total=" . $total52 . "\n";
    outputsubtotal=40
    tax=12
    total=52