PHP如何制作这样的函数?第一() - > callSecond();?

时间:2016-06-15 02:35:51

标签: php function

我尝试在下面创建一个这样的函数,如何做到这一点,任何一个例子?

<div id="wrapper">
  <div class="box">
    <div class="header">
      <p>Header</p>
    </div>
    <div class="content">
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
      <p>Content here Lorem Ipsum</p>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

  <?php
     class OtherClass{
        public function callSecond(){
            echo 'Second Called';
        }  
     }

    class MyClass{
        public function first(){
            return new OtherClass();
        }
    }

    $myClass = new MyClass();
    $myClass->first()->callSecond();
?>

答案 1 :(得分:1)

您只需要一直返回对象。它被称为fluent interface。它可以是自我或其他对象。

<php
class A
{
    public function first()
    {
        // Do something

        return $this;
    }
    public function callSecond()
    {
        // Do somewthing else

        return $this;
    }
}

$a = (new A())->first()->callSecond();