NotionCommotion Posted June 8, 2016 Share Posted June 8, 2016 For instance, I can do this. $o=new MyObj(); $stuff=$o->getStuff(); $a=$stuff->a; $b=$stuff->b; $c=$stuff->c; Or should I do this? $o=new MyObj(); $o->setStuff(); $a=$o->a; $b=$o->b; $c=$o->c; Or maybe this? $o=new MyObj(); $a=$o->getA(); $b=$o->getB(); $c=$o->getC(); If one of the latter two, should setStuff() return $this so I can chain it? $a=$o->setStuff()->getA(); I recognize every situation is different, and am just looking for typical best practices. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/301316-should-methods-typically-return-multiple-objects/ Share on other sites More sharing options...
Jacques1 Posted June 8, 2016 Share Posted June 8, 2016 Please, no more meaningless fantasy code. You've already acknowledged that your abstract examples aren't helpful and lead to a lot of misunderstands, yet you keep making them up. Nobody expects you to upload your entire project, but we do need more than “MyObj” and “stuff” to make an informed decision. Deciding between a single getter and multiple getters depends on how the data is related. This question is unanswerable when the “data” is “a”, “b” and “c”. With this little info, all I can say is that the setStuff() method makes no sense. Methods beginning with “set” are supposed to actually set a new value from external data. But your “setter” is really a weird implementation of a getter. Quote Link to comment https://forums.phpfreaks.com/topic/301316-should-methods-typically-return-multiple-objects/#findComment-1533494 Share on other sites More sharing options...
NotionCommotion Posted June 8, 2016 Author Share Posted June 8, 2016 Please, no more meaningless fantasy code. You've already acknowledged that your abstract examples aren't helpful and lead to a lot of misunderstands, yet you keep making them up. Nobody expects you to upload your entire project, but we do need more than “MyObj” and “stuff” to make an informed decision. Deciding between a single getter and multiple getters depends on how the data is related. This question is unanswerable when the “data” is “a”, “b” and “c”. With this little info, all I can say is that the setStuff() method makes no sense. Methods beginning with “set” are supposed to actually set a new value from external data. But your “setter” is really a weird implementation of a getter. I am just starting to think about this, haven't attempted to do anything with it, and unfortunately do not have any real examples. Okay, I see what you mean about deciding between a single getter and multiple getters depending on how the data is related, and there is no way anyone could answer without more details. Does a setter just set some property inside the object? Are PDO::prepare() and PDO::execute() setters, and PDO::fetch() a getter? If a setter is first executed, is it typically considered better practice to access the property directly, or through some method (which is a getter?) which then returns the property? Does a setter typically return the object (i.e. return $this;)? Quote Link to comment https://forums.phpfreaks.com/topic/301316-should-methods-typically-return-multiple-objects/#findComment-1533498 Share on other sites More sharing options...
requinix Posted June 8, 2016 Share Posted June 8, 2016 Chaining is a matter for coding style. Some people like it. Some people don't. You need to decide whether you like it or not and then write your code consistently accordingly. As for returning an object/array: It depends. Thus why abstract examples aren't very helpful for us. If getStuff() does not mutate (alter) the object and really does have multiple values to return then sure, return an object/array with the values. If setStuff() does mutate the object and a/b/c are all attributes of the object which you may want to access then it makes sense to have getter functionality, and whether that's ->a or getA() is another matter for coding style. Quote Link to comment https://forums.phpfreaks.com/topic/301316-should-methods-typically-return-multiple-objects/#findComment-1533501 Share on other sites More sharing options...
Jacques1 Posted June 8, 2016 Share Posted June 8, 2016 Does a setter just set some property inside the object? Are PDO::prepare() and PDO::execute() setters, and PDO::fetch() a getter? Neither. A setter assigns a new value to a property and typically starts with “set...”, a getter retrieves the value of a property and typically starts with “get...” (or “is...” for boolean properties). This doesn't apply to any of the methods you've mentioned. As a better example: A User instance might have a User::setEmailAddress($newAddress) setter and a User::getEmailAddress() getter. The setter sets the e-mail address, the getter gets it (just like the names say). If a setter is first executed, is it typically considered better practice to access the property directly, or through some method (which is a getter?) which then returns the property? If you have a setter, that implies you don't want the property to be public. That's the whole point of getters and setters: They allow controlled access to a private or protected property. So either the property is public and may be changed to anything by anyone, in which case you don't need getters and setters. Or the property is non-public, in which case getters and (if needed) setters are simply a requirement. Does a setter typically return the object (i.e. return $this;)? It typically doesn't return anything. But nobody prevents you from returning $this (e. g. for the sake of a Fluent Interface). Quote Link to comment https://forums.phpfreaks.com/topic/301316-should-methods-typically-return-multiple-objects/#findComment-1533502 Share on other sites More sharing options...
NotionCommotion Posted June 9, 2016 Author Share Posted June 9, 2016 Thank you requinix, You point about whether a point mutates make sense.Thank you Jacques1,Your replies were helpful. Quote Link to comment https://forums.phpfreaks.com/topic/301316-should-methods-typically-return-multiple-objects/#findComment-1533511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.