php - Setters and objects -


i've added method class:

/**  * @param string|streamableinterface $body  * @return message  */ public function setbody($body) {     $this->body = $body instanceof streamableinterface ? $body : new stringstream($body, true);     return $this; } 

and occurs me semantics little bit different depending on if pass method string or object.

if give string, construct new object, otherwise create reference object provide, means can further modify object outside class in 1 case, not other.

is bad practice? should create full clone of object if pass 1 in? i'm not sure expected behaviour is.

in opinion, stream shouldn't cloned , should keep code way is. consider have done best practice. if wants object have own isolated stream, should give object own stream—i leave decision caller.

your method commonly done in every similar scenario have seen.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -