Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

When creating my object I pass it an array of a row from my database. (everything in the array we will need, disregarding unnecessary elements at sql query level)

When I need to access certain array elements from within my class, I do so like

$this->row['element']

However, As I continue development, I sometimes forget what exactly is in this passed array.(this itself doesn't seem good)

I am wondering if their is a professional approach to dealing with this, Or am I the only one who has these "I wonder whats in the array" thoughts.

One approach to tackling this could be that when we originally pass the array, in the constructor, we assign each element of the array to its own variable, but is this considered professional practice?

Additionally by doing this, we could make those variables constants, in a attempt at immutability.

Overall I am trying to adhere to good software craftsmanship.

Regards.

share|improve this question
1  
They're called "magic strings." In a proper OO language, you would create a class that holds the elements of your row. You could then take advantage of intellisense in your IDE to pick the element you want from the row object. –  Robert Harvey Jan 30 at 0:15
    
Just read about that term, So you're saying I should dissect the array into singular variables, Curious as to why all the OOP and programming books have never explicitly touched on this, nor mentioned that term in relation with the issue. –  cecilli0n Jan 30 at 0:22
    
Probably because PHP programmers have been doing it the way you described for a long time, and online PHP tutorials (and most books, I would imagine) just parrot the same approach. Have a look here: css.dzone.com/books/practical-php/practical-php-patterns-data –  Robert Harvey Jan 30 at 0:26
    
...although I do note that is a whole lot of ceremony for the simple problem of packaging data rows. In c#, we would simply use Properties, not getters and setters. –  Robert Harvey Jan 30 at 0:31
    
The books/tutorials I have been reading mostly use Java/C++ as their language of example, But you are right it does seem alot of overhead to define each element in the array into its own property, which is why I haven't started using this approach and decided to seek advice here. Going to read that link you gave now. Thanks –  cecilli0n Jan 30 at 0:38

1 Answer 1

The best way is to thoroughly describe the structure of your arrays within comments in your code, but I use var_dump() at times that I forget. It will dump a detailed list of each element within an array or object (name, type, key, value, etc) including deeply nested arrays. I've been using PHP for more than 10 years so this sort of thing is second nature to me (PHP didn't really have debuggers or high-end IDE's back than, so you were stuck digging through vars manually)

And yes, it's OK to manually name elements within array properties when constructing a class object. This is actually common practice in configuration files. For example, a config class can have the property $database, and then set $this->database['user'], $this->database['pass'], etc within the constructor.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.