Hi all,
So… I’ve been lured to the dark side: I’m beginning down the long road of Object-Oriented Programming. I think I understand the concepts pretty well, (read: think), but I’ve got a particular function in my code that doesn’t seem to be working.
My idea is to create a class called DB_Row() in which a user can input a string of “fields” separated by a comma. The idea behind this is to create a ‘false’ database, made out of arrays. (I know the idea is stupid, I’d just like to do it). So, for example, if I typed
DB_Row("mike, steve, fred");
I’d get back an array of those ‘fields’ so that field 0 would be set to mike, field 1 to steve and field 2 to fred.
I have the majority of this class working, except for a getField() function I’m writing in which you’d type something like:
getField("steve");
And it’d return that field’s value to work with further. Here’s the code I have:
class DB_Row {
var $row_fields_str;
var $row;
function db_row($row_field_str) {
$this->row_fields_str = $row_field_str;
$this->row = $row;
$this->row = explode(',', $this->row_fields_str);
}
function printRow() {
print_r($this->row);
}
function getFieldCount() {
return count($this->row);
}
function getField($field) {
foreach($this->row as $value) {
if($value == $field) {
echo $field;
}
}
}
}
$testRow = new DB_Row("mike, steve, fred");
$testRow->printRow();
echo '<br />' . $testRow->getFieldCount() . '<br />';
echo '<br />' . $testRow->getField("steve");
You can see the results of this at http://www.kuklamusic.com/oop2.php
I should mention that if I set the foreach to $field = $value
, I get the $field value three times. I was hoping it would loop over the array, come to “steve” in the 2nd item in the $row array and spit it out…
I hope this makes sense, and I apologize if it’s cryptic.
Thanks very much!
Steve K.