masterprompt Posted February 1, 2008 Share Posted February 1, 2008 o I'm a self taught php guy (like most of you I assume) and after tons of google searching I can't find the answer to two questions, so any smart php guys out there can you help me out? Question 1: So I have a object, say like this class MyObject { var Test1 var Test2 var Test3 } And I refference this by going ThisObject = New MyObject ThisObject->Test1 = 1 ThisObject->Test2 = 2 ThisObject->Test3 = 3 is there any way to do this quickly, say with a for next loop? Like: While (x<=3) { ThisObject->Test[x] = x } ???????? Question 2: Is there a way to loop through all available structure varables available publicly? Like a Foreach(ThisObject ->Variables as ThisVar) { ThisIsTest1=ThisVar //Next will be Test2 } Or even a list of the local structure variables to see if something exists (or search for it) in code? The first question is really important as I want to save a few lines of code in quite a few places, the structure I am pulling the data from isn't of my creation, otherwise I would have put it into an array and returned it for such a reason as to save on code. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/88953-advanced-php-and-objects-help/ Share on other sites More sharing options...
rhodesa Posted February 1, 2008 Share Posted February 1, 2008 Why don't you just use an array? <?php class MyObject { var Test = array(); } $ThisObject = New MyObject(); $ThisObject->Test[] = 1; $ThisObject->Test[] = 2; $ThisObject->Test[] = 3; foreach($ThisObject->Test as $val){ echo $val; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88953-advanced-php-and-objects-help/#findComment-455587 Share on other sites More sharing options...
masterprompt Posted February 1, 2008 Author Share Posted February 1, 2008 My point is that it's someone else's structure, and I'm actually reading said Testx values... I want to read them in enum... So MyVar[] = MyObject->Test1 MyVar[] = MyObject->Test2 MyVar[] = MyObject->Test3 MyVar[] = MyObject->Test4 Can I shrink this down any? Like a simple loop solution? Bare in mind I have 3 other functions that happen between each of those that are all the same, so if there were a simple way to simplify this without having to directly say "Test1" or "Test2" and so on (like say Testx or something) I just hate having to write 'repetitive' code, and the only difference here is the Test1, Test2, etc... Quote Link to comment https://forums.phpfreaks.com/topic/88953-advanced-php-and-objects-help/#findComment-455590 Share on other sites More sharing options...
rhodesa Posted February 1, 2008 Share Posted February 1, 2008 How bout this? <?php $MyVar = array(); for($n = 1;1;$n++){ //Infinite loop $varname = 'Test'.$n; if(!isset($MyObject->$varname)) break; //Exit loop $MyVar[] = $MyObject->$varname; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88953-advanced-php-and-objects-help/#findComment-455604 Share on other sites More sharing options...
masterprompt Posted February 1, 2008 Author Share Posted February 1, 2008 I didn't even know that was allowable!!!!! If that works that will save a load of code writting!!! Ok, spin a question off that, given that answer, what if I wanted to loop through all of the variables???? Like if I had the following: Class MyObject { var Bigthing; var Medthing; var smallthing; } $ThisObject = new MyObject(); and I wanted to loop through all the vars of that object? Like a foreach($ThisObject as $Thisvar) would $Thisvar contain each var like an array? I want to get each var value, but I need to know what the var is named as well (because in my case I need to record if it's small, med, or large). Quote Link to comment https://forums.phpfreaks.com/topic/88953-advanced-php-and-objects-help/#findComment-455619 Share on other sites More sharing options...
rhodesa Posted February 2, 2008 Share Posted February 2, 2008 Interesting, I was just looking at the PHP Doc (http://us.php.net/manual/en/language.oop5.iterations.php) and it seems like in PHP5 you can use a foreach and it will work on all public variables. Quote Link to comment https://forums.phpfreaks.com/topic/88953-advanced-php-and-objects-help/#findComment-455723 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.