Beginning OOP Programming

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.

(I know the idea is stupid, I'd just like to do it)

Everyone’s gotta start somewhere, but I agree with you when you say it’s a pretty pointless example, mainly because you’re making things pretty complex on yourself, when, say, a simple array could have done the exact same trick. Nonetheless, it gets you started.

The question I’d like to ask, is: what do you want the getField() to process, i.e. what do you want to input and what should it return? I see you inputting “steve”, the name of the value, and you’re expecting … the key of that array’s element (i.e. “1”)? If so, you could try using array_search(), rather than loops. Loops + arrays can get really tricky and complex.

Hi all,

I’m wondering if anyone can point me toward some resources which will help me get a grasp on how objects can interact with one another to pass, receive and use information. Here’s an example of what I mean:

Right now, I’ve got a class called DB_Row() with which I can do something like this:

$row1 = new DB_Row("page_id = 1, page_name = Index")

Effectively taking the information after each ‘=’ and storing it under a ‘key’ of the the information before the ‘=’. (So, we have something like $fields_arr[‘page_id’] = 1, $fields_arr[‘page_name’] = Index)

I can then use the following methods:

printFields()       // returns the print_r of $fields_arr
countFields()       // returns the count() of $fields_arr
getField($field)    // returns the field whose key is equal to $field

Now, what I’d like to do is to use instances of the DB_Row class inside of another, larger object, Page(), where each Page object would have a corresponding DB_Row() object

(The reason I made DB_Row() into an object, instead of incorporating it into Page() is that I also plan to have an object larger than page called Section() which will be a group of Page objects.)

Hopefully this makes some sense.

Any advice is greatly appreciated!

Steve K.

Sponsor our Newsletter | Privacy Policy | Terms of Service