NotionCommotion Posted May 11, 2017 Share Posted May 11, 2017 I need a method which accepts an array, and returns the property values of $this who's keys are the values in the array. I am thinking of making it more generic, and instead of using the property values of $this, pass it an object to be used. But this would mean doing the following: $subset=$this->getObjectSubset($this, ['a','b','c']); protected function getObjectSubset($source, array $arr) { $obj=new \stdClass; foreach($arr as $prop){ if(isset($source->{$prop})) $obj->{$prop}=$source->{$prop}; } return $obj; } Since objects are passed by reference, I guess there is nothing wrong with it. But it just smells a little wrong passing $this, so would like a second opinion. Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted May 11, 2017 Share Posted May 11, 2017 Either keep it an instance method and don't pass this, or make it static and do. Probably the former. Or forget the method entirely and go with $subset = (object)array_intersect_key(get_object_vars($this), array_flip(['a', 'b', 'c'])); Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 11, 2017 Author Share Posted May 11, 2017 I didn't show it as I didn't think it was relevant, but if the array map was ['a','b','c'=>'d'], I would return (object) ['a' => $this->a, 'b' => $this->b, 'd' => $this->c]. As such, your slick line of code might get a little too complicated. What do you mean by "make it static"? Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted May 11, 2017 Share Posted May 11, 2017 Can't find any good explanations of static methods... Wikipedia? class Math { public static function max($a, $b) { return $a >= $b ? $a : $b; } } echo Math::max(1, 2); static I didn't show it as I didn't think it was relevant, but if the array map was ['a','b','c'=>'d'], I would return (object) ['a' => $this->a, 'b' => $this->b, 'd' => $this->c]. As such, your slick line of code might get a little too complicated. $subset = (object)array_intersect_key(get_object_vars($this), array_flip(array_keys(['a', 'b', 'c' => 'd'])); Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 11, 2017 Author Share Posted May 11, 2017 Guess it isn't any more complicated! Yes, I understand static methods. Why is it okay to pass $this to a static method and not an instance method? Quote Link to comment Share on other sites More sharing options...
requinix Posted May 11, 2017 Share Posted May 11, 2017 (edited) A static method is about doing something without needing access to a particular instance. Math::max does not need an instance of a "Math" class to do its work, so it is static. An instance method is... not that. Since your method does need $this, or at least its work is dependent upon having an instance of that class, a static method is not as appropriate as an instance method. protected function getObjectSubset(array $arr) { $obj=new \stdClass; foreach($arr as $prop){ if(isset($this->{$prop})) $obj->{$prop}=$this->{$prop}; } return $obj; }Note you don't need {}s for simple dereferencing like you're doing - $this->$prop is fine. [edit] For the record, I'd go for the foreach instead of the one-liner. Easier to read and understand, and it's not like those nested array_* calls will give any big performance boost. [edit 2] That one-liner won't actually do what you want anyways. Forget it. Edited May 11, 2017 by requinix Quote Link to comment 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.