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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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