I\'m trying to make a PHP (5) object that can iterate through its properties, building an SQL query based only on its public properties, not its private ones.
As this pa
If you cast the object to an array before iterating over it, the private and protected members will have special prefixes:
class Test{
public $a = 1;
private $b = 1;
protected $c = 1;
}
$a = new Test();
var_dump((array) $a);
displays this:
array(3) {
["a"]=>
int(1)
["Testb"]=>
int(1)
["*c"]=>
int(1)
}
There are hidden characters there too, that don't get displayed. But you can write code to detect them. For example, the regular expression /\0\*\0(.*)$/
will match protected keys, and /\0.*\0(.*)$/
will match private ones. In both, the first capturing group matches the member name.