Jack Hales
Jack Hales

Reputation: 1664

How to call a class function from a instance call?

So I currently have a class and wanted to make class calls cleaner by being able to call the class instance as a function, like the __toString magic method which outputs a string when called, instead be able to call $instance() as a function and have it called.

Like:

class MyClass {
  public function __onCall() {
    echo 'This was called when the user called the instance!';
  }    
}

$instance = new MyClass();
$instance();
//opts: This was called when the user called the instance!

Essentially I want to be able to chain class functions and cut off one chain that'll be called a lot by calling a function from the instance.

Syntax I want:

$Class('Some String')->SomeFunction()->AnotherFunction();

Syntax I have:

$Class->select_string('Some String')->SomeFunction()->AnotherFunction();

:-)

Upvotes: 0

Views: 434

Answers (1)

Aldee
Aldee

Reputation: 4538

Ok. I see what you mean here. So you want to have a fluent method chaining. This is actually answered by another thread

Hope this helps.

Upvotes: 1

Related Questions