Jump to content

Advanced PHP and objects help


masterprompt

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/88953-advanced-php-and-objects-help/
Share on other sites

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;
}
?>

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... 

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).

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.