search

Home  >  Q&A  >  body text

Questions about php objects.

When using the API of KIRBY (a CMS), I found the following writing method:

$page->children()->visible();

What does this way of writing mean? Is it a nested function within a function in an object?

淡淡烟草味淡淡烟草味2822 days ago577

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-31 10:36:18

    I haven’t seen its source code, but it’s like this, $page->children() returns an object, and this object has the method visible(). So you can call it like this, which is also called chain call.

    Give me an example

    class Wallet
    {
        protected $money;
        
        public function money()
        {
            $this->money = new Money();
            return $this->money;
        }
    }
    
    class Money
    {
        protected $total;
        
        public function used($count)
        {
            $this->total -= $count;
        }
    }

    You can chain calls like this

    $user = new User();
    $user->money()->used(23);

    reply
    0
  • Cancelreply