0

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

yivi
  • 42,438
  • 18
  • 116
  • 138
lucky
  • 1
  • 1
  • 1
    Could you please describe your purpose of this question. what do you need to do specifically with this approach and why you need to follow this using `static` members? – Faesal Dec 25 '21 at 02:37

1 Answers1

0

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()