Jump to content

Passing $this to a method


Recommended Posts

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

 
Link to comment
Share on other sites

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']));
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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']));
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.