You'll want to use get_class_vars()
, which returns an associative array of all properties (class vars) of the provided class, with the var name as the key. Use get_class_vars( get_called_class() )
from within the class to get that class' name as a string. get_class( $this )
works as well.
Of course, this gives you all the properties of the class (regardless of access control modifier), so chances are you want to also filter this list. An easy (if somewhat obfuscated) way to do this is to use array_intersect_key()
along with array_flip()
as per this SE Answer. So the whole thing looks like:
array_intersect_key( get_class_vars( get_called_class() ), array_flip( array( 'var_1', 'var_2', 'var_3' ) ) );
You'll have to decide if the resulting code is really worth it. It's probably easier to read array( 'var_1' => $this->var_1, 'var_2' => $this->var_2 );
as @ircmaxell points out.
However, because a) I like Yak Shaving and b) because I am a very industrious lazy programmer and c) there was probably a one-liner in Perl that did this elegantly, here's the code in the context of the problem I was trying to address: overriding an associative array (that's also a property of the current class) with a number (but not all) of other properties of the same class.
<?php
class Test {
protected $var_1 = 'foo';
private $var_2 = 'bar';
public $var_3 = 'baz';
private $ignored_var = 'meh';
public $ary = array(
'var_1' => 'bletch',
'var_2' => 'belch',
'var_4' => 'gumbo',
'var_5' => 'thumbo'
);
public function test(){
$override = array_intersect_key( get_class_vars( get_called_class() ), array_flip( array( 'var_1', 'var_2', 'var_3' ) ) );
return array_merge( $this->ary, $override );
}
}
$test = new Test();
var_dump( $test->test() );
Which produces the expected output:
array(5) { ["var_1"]=> string(3) "foo" ["var_2"]=> string(3) "bar" ["var_4"]=> string(5) "gumbo" ["var_5"]=> string(6) "thumbo" ["var_3"]=> string(3) "baz" }
Note that in my own usage, I wouldn't have broken this out into 2 lines, but I wanted to keep the code that refers to the OP separate.