How to build multi oop functions in PHP5?

The key to chaining methods like that within your own classes is to return an object (almost always $this ), which then gets used as the object for the next method call Like so: class example { public function a_function() { return $this; } public function first($some_array) { // do some stuff with $some_array, then... return $this; } public function second($some_other_array) { // do some stuff return $this; } } $obj = new example(); $obj->a_function()->first(array('str', 'str', 'str'))->second(array(1, 2, 3, 4, 5)) Note, it's possible to return an object other than $this and the chaining stuff above is really just a shorter way to say $a = $obj->first(...); $b = $a->second(...) minus the ugliness of setting variables you'll never use again after the call.

The key to chaining methods like that within your own classes is to return an object (almost always $this), which then gets used as the object for the next method call. Like so: class example { public function a_function() { return $this; } public function first($some_array) { // do some stuff with $some_array, then... return $this; } public function second($some_other_array) { // do some stuff return $this; } } $obj = new example(); $obj->a_function()->first(array('str', 'str', 'str'))->second(array(1, 2, 3, 4, 5)); Note, it's possible to return an object other than $this, and the chaining stuff above is really just a shorter way to say $a = $obj->first(...); $b = $a->second(...);, minus the ugliness of setting variables you'll never use again after the call.

3 Just FYI: This technique is used to create a Fluent Interface (en.wikipedia. Org/wiki/Fluent_interface). – Dennis Haarbrink Jul 21 '10 at 11:39 Hmmm not more?

And will all stuff be handle normaly? :) – NeoNmaN Jul 21 '10 at 11:40 @Dennis Haarbrink: The name had slipped my mind. Thanks :) – cHao Jul 21 '10 at 11:40 @NeoNmaN: Depends on what you're trying to do, of course.

If your intent is to call a bunch of methods on a single object, then yes, this is how it's done. Like I said, though, it's possible to return something besides $this. If you do so, and especially if what you're returning isn't the same type or is totally unrelated to $this, that's a whole other thing than this -- and it's just shorthand for that last bit of grey code in my answer.

– cHao Jul 21 '10 at 11:49.

$object->function()->first(array('str','str','str'))->secound(array(1,2,3,4,5)); This isn't strictly valid PHP, but what this is saying is... You are calling a method on the $object class that itself returns an object in which you are calling a method called first() which also returns an object in which you are calling a method called second(). So, this isn't necessarily just one class (although it could be) with one method, this is a whole series of possibly different classes. Something like: class AnotherClass { public function AnotherClassMethod() { return ' } } class MyClass { public function MyClassMethod() { return new AnotherClass(); } } $object = new MyClass(); echo $object->MyClassMethod()->AnotherClassMethod(); // .

Php - How to build multi oop functions in PHP5 - Stack Overflow.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions