Liquid Fire Posted November 9, 2007 Share Posted November 9, 2007 is there a way to loop through class members like you can do with arrays and foreach? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted November 9, 2007 Share Posted November 9, 2007 Yes, it only works on public properties though. class test{ public $test = 'test'; public $test2 = 'test2'; } $x = new test(); for($x as $val){ echo $val .'<br />'; } Quote Link to comment Share on other sites More sharing options...
Liquid Fire Posted November 9, 2007 Author Share Posted November 9, 2007 there is no way to do it on protected members within the class itself? I almost never use public members because it kinda defeats one of the big purposes of classes in hide data from everyone, i always just use protected. Quote Link to comment Share on other sites More sharing options...
SilveR316 Posted November 9, 2007 Share Posted November 9, 2007 You can loop through $this within the class itself to get its members. Quote Link to comment Share on other sites More sharing options...
Liquid Fire Posted November 10, 2007 Author Share Posted November 10, 2007 I actually have an array that has the class members as the keys and that points to another array with information relating to the database, i am just going to use that since every class that extends from base_data needs that and that is the only class i need to be able to do the merge function on(which requires the looping). On a side note for anyone else might be wanting this, you can use the ReflectionClass to get the properties of a class, it is a pretty nifty class for getting class information. Quote Link to comment Share on other sites More sharing options...
aschk Posted November 12, 2007 Share Posted November 12, 2007 As far as I know, ReflectionClass is buggy (as of 5.1) and hasn't had a stable release yet? Btw this is a question rather than definitive answer. I also thought that Reflection would only get public variables. Or at least that was the problem I was having with it last time I used it. Hopefully that's all fixed now. I do feel however that you might as well make the internal members public if they are needed to be accessible, else you are sort of circumventing the reasoning behind member typing. Might as well go back to PHP 4 I don't know for sure as i can't see what it is you're attemping to do. Quote Link to comment Share on other sites More sharing options...
Jenk Posted November 12, 2007 Share Posted November 12, 2007 I don't see why you'd want to access private/protected member variables because that is the point of them being private/protected... to be private and/or protected. What are you trying to achieve? Quote Link to comment Share on other sites More sharing options...
448191 Posted November 14, 2007 Share Posted November 14, 2007 I actually have an array that has the class members as the keys and that points to another array with information relating to the database, i am just going to use that since every class that extends from base_data needs that and that is the only class i need to be able to do the merge function on(which requires the looping). This is not making any sense to me. If you need to transfer data from a business object to the data layer, use a DTO. Basically you assemble an object from within a scope where the properties are visible (in the class itself or in the hierarchy), then provide an interface to fetch the transfer object. The data layer needs to know how to fetch the information from the DTO. Common practice would be to use default 'getters'. This way you don't directly expose the members of the business object, yet still have a mechanism to retrieve the data to map to the database. The same can be done to reverse the process, i.e. restore the state of an object from a DTO created from persistent data out of the database. 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.