How to terminate last function in chaining method php

65 Views Asked by At

I got some problem and I don't know how to fix it.

this is sample for the problem

class DancingClass {
    private static $associate = [];
    private static $first;

    public static function first($param) {
        self::$first = $param;

        return new self;
    }

    public function second($param) {
        self::$associate["second"] = $param;

        return new self;
    }

    public function finish() {
        var_dump(self::$associate["second"]);
        $sec = self::$associate["second"] | "";

        $all = self::$first . " ditemani oleh " . $sec;

        return $all;
    }
}

Then I call with chaining method

$callingClass = new DancingClass;

echo $callingClass::first("lucky")->second("adhitya")->finish(); // Return "lucky ditemani oleh adhitya"
echo "<br/>";

echo $callingClass::first("fatur")->finish(); // Return "fatur ditemani oleh"

but I got result like this the result

1

There are 1 best solutions below

1
Dominik On

When you call second() method it sets variable on the same class instance that you call later.

Maybe you should try:

echo ((new DancingClass())->first(...)->second(...)->finish();
echo ((new DancingClass())->first(...)->finish()