3

Possible Duplicate:
PHP method chaining?

I occasionally see some php applications use classes like that:

$Obj = new Obj();
$Obj->selectFile('datafile.ext')->getDATA();

The example above gets the contents of the selected file then returns them ( just an example );

Well, before i decided to ask you how can I do this, I tried this:

class Someclass {
    public function selectFile( $filename  ) { 
       $callAclass = new AnotherClass( $filename );
       return $callAclass;
    }

    // Other functions ...
}

class AnotherClass {
    protected $file_name;

    public function __construct ( $filename ) { $this->file_name = $filename; }
    public function getDATA ( ) {
        $data = file_get_contents( $this->file_name );
        return $data;

    } 

    // funcs , funcs, funcs ....

}

Is that the right way to accomplish this task? And please tell me what these classes are called.

Community
  • 1
  • 1
Innogen
  • 49
  • 1
  • 5
  • 3
    Google for `method chaining in php`. Basicly You just return `$this` in chained method :) Tho the example is just missleading, select file probably is just a `factory` to product another object based on the context (in this name filename). So if method returns any type of Object You can chain it. – Bartosz Grzybowski Feb 20 '12 at 22:08
  • 1
    Please refrain form using emoticons, internet slang and bold text here on stack overflow. Also to remember to read the [FAQ](http://stackoverflow.com/faq) – Richard J. Ross III Feb 20 '12 at 22:08
  • I like the term train wreck for this. – phpmeh Feb 20 '12 at 22:19

2 Answers2

11

It's called method chaining. Have a look at this question on SO.

Here's a way you can do what you're trying to achieve:

class File 
{
    protected $_fileName;

    public function selectFile($filename) 
    { 
        $this->_fileName = $filename; 
        return $this;
    }

    public function getData()
    {
        return file_get_contents($this->_fileName);
    }
}


$file = new File();
echo $file->selectFile('hello.txt')->getData();

Notice we return $this in the selectFile, this enables us to chain another method onto it.

Community
  • 1
  • 1
Flukey
  • 6,445
  • 3
  • 46
  • 71
  • so i should've just return the _Object_ itself ! Thanks a-lot ! you're so generous ! **Bless you :)** – Innogen Feb 21 '12 at 17:14
3

The above example (the first one) is something called chaining. Here's a regular class:

class a_class
{

    public function method_a()
    {
        echo 'method a';
    }

    public function method_b()
    {
        echo ' - method b';
    }

}

You'd call those like this:

$class = new a_class();

$class->method_a();
$class->method_b();

Which would echo 'method a - method b'. Now to chain them, you'd return the object in the methods:

class a_class
{

    public function method_a()
    {
        echo 'method a';
        return $this;
    }

    public function method_b()
    {
        echo ' - method b';
        return $this;
    }

}

You'd call those like this:

$class = new a_class();

$class->method_a()->method_b();

Which would also echo 'method a - method b'.

Although I have returned the object in the last method - you strictly wouldn't need to, only the preceding methods require that for chaining.

BenOfTheNorth
  • 2,904
  • 1
  • 20
  • 46