1

Say I have the following class:

class Test
{

    private static $instance = false;

    public static function test()
    {
        if(!self::$instance)
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function test1()
    {
        //...
    }

    public function test2()
    {
        //...
    }

}

And I go about calling functions by chaining them like so:

$data = Test::test(...)->test1(...)->test2(...);

At the moment for the above method chain to work I have to keep returning $instance and I really would like it if I could return something from test2() to then be assigned to $data but I am not sure how to do this as I have to keep returning $instance in order for mt method chain to work?

Erdss4
  • 1,025
  • 3
  • 11
  • 31
  • 1
    You'll have to choose: Either you return the object so that you can chain, or you return a value. You can't really do both. – jeroen Mar 05 '17 at 21:21

4 Answers4

1

If you want to chain methods, you need to return the current instance from any method which has another call chained on after it. However, it's not necessary for the last call in the chain to do so. In this case that means you're free to return whatever you like from test2()

Just bear in mind, if you return something different from test2() you'll never be able to chain anything onto it in future. For example, $data = Test::test(...)->test2(...)->test1(...); wouldn't work.
Protip: it's worth documenting your code with some comments explaining which ones are and aren't chainable so you don't forget in future.

lucas
  • 1,910
  • 2
  • 21
  • 25
1

Generally speaking if you are doing method chaining, and I'm assuming each of the tests above return your data model in a different state, and I assume that you want some data from the model itself. I would do the following:

class Test
{
    private static $model;

    public function test1() {
        //do something to model
        return $this;
    }

    public function test1() {
        //do something to model
        return $this;
    }

    public function finish_process() {
        //process results
        return $this.model;
    }
}

so essentially i can do the following now:

$results = Test::test1()->finish_process();

and

$results = Test::test1()->test2()->finish_process();
Sami Syed
  • 11
  • 2
1

You can pass the $data by its reference and you can change it or assign any data into it.

// inside class

public function test2( &$data ) {
   $data = 'it will work';
}

// outside class
 $data = '';
 Test::test(...)->test1(...)->test2($data);

check this http://php.net/manual/en/language.references.pass.php

Sudhanshu Jain
  • 494
  • 3
  • 11
0

return $this inside test1() and test2() methods.

FatBoyXPC
  • 861
  • 7
  • 15