0

I just need explaining or a push in the right direction how to search this: what are the submethods(?) for some class methods?

I've seen calls like:

$data = Log::Bank($var1, $var2)->start();
$data = Log::Bank($var1, $var2)->refund();

or like

$xml = getData($uri)->toArray();

what are the start(), refund() and toArray() options?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Peon
  • 7,902
  • 7
  • 59
  • 100

2 Answers2

1

These methods return objects on which there are other methods and there are called as well. So getData will return some object that has toArray() method.

You can as well return object itself and use method chaining, i.e.

class MailBuilder {
private $subject;
private$ $body;

public function setSubject($body) {
  $this->body = $body;

  return $this; 
}

public function setBody($body) {
  $this->subject = $subject;

  return $this; 
}
}

and then you could do something like:

$builder = new MailBuilder();
$builder->setSubject('something')->setBody('something');

So you can do that because the methods are returning the object itself and though you can just chain calls to next methods.

Remember that code like that (calling objects nested too deeply) is not good (its called a train wreck):

$object->someMethodReturningObject()->someOtherMethod()->nextObjectMethod()
Tom St
  • 908
  • 8
  • 15
  • Do you have any link to tutorial where I can read and understand how they work and how they are created? – Peon Dec 07 '16 at 14:06
  • I'd suggest this to get a good grasp of oo in php and the documentation is really good for that - http://php.net/manual/en/language.oop5.php you can checkout some other tutorials if the format doesnt suit you, just search php oo tutorials... btw. $this in a object (class) context will return instance of that object (MailBuilder in my example) – Tom St Dec 07 '16 at 14:15
1

These are not submethods, they are method chaining. In this instance, Log::Bank($var1, $var2) will return an object. The object that gets returned from that method has a start() method on it that you can use. So when you add ->start() at the end then you're running it on the returned object.

CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50