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

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? Commented Dec 25, 2021 at 2:37

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

it's running, but still got same result

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.