Forum Moderators: coopster

Message Too Old, No Replies

Check if property is dynamic

         

lorum

9:12 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Hello,

I am currently creating dynamic properties in a class and I would like to iterate only through these properties, not the predefined properties. Is there a way to do this?

Example:


class Example{
var $Var1 = "hi";
var $Var2 = 3;
}

$class = new Example();

$class->var3 = "Dynamic property";
$class->var4 = "Another dynamic property";

// This prints all properties, but I just want var3 & var4
foreach($class as $key => $value) {
print "$key => $value";
}


PS. The predefined properties are always upper case in their first letter. The dynamic ones are never.

eelixduppy

9:19 pm on Apr 28, 2010 (gmt 0)



Is this even possible to do? Because var3 and var4 are not part of the Example object...

Even if this is possible, it's poor OO programming. I would stick away from this.

If you need to iterate through something, then I would create a public array in the class definition:

class Example{ $Var1 = "hi"; $Var2 = 3; public $values = array(); }


Then when you want to add or whatever to that array you can do so:

$instance->values[] = "Dynamic property";
$instance->values[] = "Another one";