The question I want to ask you all is do you treat programming PHP applications as you were programming in a "forced-OOP" language. What I'm basically getting at is, in compaison to C#, do you write your code the same?? In C#, other .NET languages and other "forced-OOP" languages, everything is an object, contained within a class (and depending on the language, contained within a namespace - But I won't go into that).
In PHP, it's more than common practice to create a, for example, Database layer class. Such class would provide methods such as ->Query, ->Row_Count, ->Get_Records, along with several other common database functions. If you was to be writing this class, what would you return from them?? The most common way would be to do what your instinct says, due to the very loosely-typed nature of PHP, and return an array, or a raw resultset, depending on the method.
Example (note this is PURELY for demonstration + just written and may not work/be accurate):
public function Get_Records($Query) {
$RawResults = $this->Query($Query);
$Return = array();
while($RawRecord = mysql_fetch_assoc($RawResults)) {
$Return[] = $RawRecord;
}
return $Return;
}
The second option would be to do it the true OOP way, which would be to create a class that represented either a single result or a set of results, along with associated methods to retrieve the data in different ways, and manipulate the data.
Example (again, PURELY for demonstration + just written and may not work/be accurate):
This is more pseudo than practical, as some methods require more data..
class Record {
private $Raw;
// Let's say, just for example, that this class is constructed with a mysql_fetch_assoc() of a row as the first argument, which could be passed/created from a Records class.
public __construct($_Raw) {
$this->Raw = $_Raw;
}
public function Get_Field($name) {
if(!isset($this->Raw[$name])) return "Not Available";
return $this->Raw[$name];
}
public function Update_Field($name, $value) {
if(!isset($this->Raw[$name])) return false;
$this->Raw[$name] = $value;
// Run a DB query to update this exact record..
}
// etc..
}
Obviously the above is only practical in certain circumstances, probably not when just retrieving a single record, however imagine mapping a whole table. Table class containing a Records class which contained several Record instances.. Data about the table's primary keys would be stored to allow for more reliable updating, etc. etc.
Anyway enough "examples" and more asking - Do you use OOP fully?

New Topic/Question
Reply



MultiQuote









|