在Class方法中调用函数并通过第一个函数获取数据?

Hi how do i create a class that works like this?

$shop = new shop();
$shop->cart(function ($data){
    //$data
})->coupon('hello world!');

$shop->getCoupon(); //hello world!

so how do i do this? i have played around with examples from Calling a function within a Class method?

i even took parts of the original title sorry original poster.

Your question is a bit vague, but I think what you are talking about is a Fluent Interface. The idea behind them is to enable you to call multiple methods on a single instance, by having each method return the instance. It's commonly used for setters on classes, and enables you to write code like:

$foo = new Foo();
$foo
    ->setThisThing()
    ->setAnotherThing()
    ->setThingToParameter($parameter)
    ...;

rather than

$foo->setThisThing();
$foo->setAnotherThing();
...

Whether you find this better or worse is a matter of taste, but Fluent interfaces do come some drawbacks

In your case, the shop class might look like:

<?php
class shop
{
  private $couponText;

  public function cart($function) {
    // Do something with $function here

    return $this;
  }

  public function coupon($couponText) {
    $this->couponText = $couponText;

    return $this;
  }

  public function getCoupon() {
    return $this->couponText;
  }
}

The key parts are the return $this; lines - they allow you to chain subsequent method calls onto each other, as in your example.

See https://eval.in/851708 for an example.