method-chaining

How to build multi oop functions in PHP5

♀尐吖头ヾ 提交于 2019-11-27 06:30:44
问题 I have a question about OOP in PHP5. I have seen more and more code written like this: $object->function()->first(array('str','str','str'))->second(array(1,2,3,4,5)); But I don't know how to create this method. I hope somebody can help me here, :0) thanks a lot. 回答1: 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() {

How can I chain my method calls?

大兔子大兔子 提交于 2019-11-27 05:58:30
问题 I have an object: var mubsisapi = { step1 : function(){alert("a")}, step2 : function(){alert("b")} } $.extend(false, mubsisapi) mubsisapi.step1().step2(); It is give step1() but not give step2() . step2() does not give an alert. How can I do this? 回答1: Not JSON, but javascript object. It's not fluent, but it can be: var mubsisapi = { step1 : function(){alert("a"); return this;}, step2 : function(){alert("b"); return this;} } $.extend(false, mubsisapi) mubsisapi.step1().step2(); 回答2: You need

How to chain functions without using prototype?

﹥>﹥吖頭↗ 提交于 2019-11-27 01:51:55
问题 I have a bunch of useful functions that I have collected during my whole life. function one(num){ return num+1; } function two(num){ return num+2; } I can call them with two(two(one(5))) But I would prefer to use (5).one().two().two() How can I achieve this without using prototype? I tried to see how underscore chain works, but their code is too intense to understand it 回答1: The dot syntax is reserved for objects. So you can do something like function MyNumber(n) { var internal = Number(n);

Method Chaining in Java [closed]

北战南征 提交于 2019-11-27 00:21:04
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . While answering a few questions on here earlier and from some work I have been doing lately I have been wondering why Java does not

Conditional Builder Method Chaining Fluent Interface

两盒软妹~` 提交于 2019-11-26 23:47:59
问题 I was wondering what would be the best way to implement a .When condition in a fluent interface using method chaining in a Builder object? For instance how would I implement the .WithSkill() and .When() methods in the following example: var level = 5; var ninja = NinjaBuilder .CreateNinja() .Named("Ninja Boy") .AtLevel(level) .WithShurikens(10) .WithSkill(Skill.HideInShadows) .When(level > 3) .Build() Update - A sample solution can be found here. 回答1: What I'd do is have NinjaBuilder keep the

Chaining Static Methods in PHP?

孤人 提交于 2019-11-26 21:58:21
Is it possible to chain static methods together using a static class? Say I wanted to do something like this: $value = TestClass::toValue(5)::add(3)::subtract(2)::add(8)::result(); . . . and obviously I would want $value to be assigned the number 14. Is this possible? Update : It doesn't work (you can't return "self" - it's not an instance!), but this is where my thoughts have taken me: class TestClass { public static $currentValue; public static function toValue($value) { self::$currentValue = $value; } public static function add($value) { self::$currentValue = self::$currentValue + $value;

How to chain method on a newly created object?

自闭症网瘾萝莉.ら 提交于 2019-11-26 16:24:47
I would like to know whether there's a way to chain methods on a newly created object in PHP? Something like: class Foo { public function xyz() { ... return $this; } } $my_foo = new Foo()->xyz(); Anyone know of a way to achieve this? In PHP 5.4+, the parser's been modified so you can do something like this (new Foo())->xyz(); Wrap the instantiation in parenthesis, and chain away. Prior to PHP 5.4, when you're using the new Classname(); syntax, you can't chain a method call off the instantiation. It's a limitation of PHP 5.3's syntax. Once an object is instantiated, you can chain away. One

How to call the same JavaScript function repeatedly while waiting for the function to run its course before invoking it again, using method chaining?

一世执手 提交于 2019-11-26 14:46:03
问题 Using method chaining, I am looking to fire a function repeatedly but only after the function has completed. Almost like don't execute until the function has fully run its course. Example of intended result: var myfunc = { copy: function(message){ window.setTimeout(function(){ console.log(message); },1000); return this; } }; myfunc.copy('hello').copy('world'); // wait a second then log: // hello // wait until first function completion (1000ms), wait a second then log: // world Any help is

Method chaining + inheritance don’t play well together?

五迷三道 提交于 2019-11-26 11:44:57
This question has been asked in a C++ context but I'm curious about Java. The concerns about virtual methods don't apply (I think), but if you have this situation: abstract class Pet { private String name; public Pet setName(String name) { this.name = name; return this; } } class Cat extends Pet { public Cat catchMice() { System.out.println("I caught a mouse!"); return this; } } class Dog extends Pet { public Dog catchFrisbee() { System.out.println("I caught a frisbee!"); return this; } } class Bird extends Pet { public Bird layEgg() { ... return this; } } { Cat c = new Cat(); c.setName(

Method chaining - why is it a good practice, or not?

拟墨画扇 提交于 2019-11-26 11:31:09
Method chaining is the practice of object methods returning the object itself in order for the result to be called for another method. Like this: participant.addSchedule(events[1]).addSchedule(events[2]).setStatus('attending').save() This seems to be considered a good practice, since it produces readable code, or a "fluent interface". However, to me it instead seems to break the object calling notation implied by the object orientation itself - the resulting code does not represent performing actions to the result of a previous method, which is how object oriented code is generally expected to