This is a discussion on overload() problem within the PHP General forums, part of the PHP Programming Forums category; I'm trying to write a class for abstracting some aspects of database programming and am running into a problem: ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm trying to write a class for abstracting some aspects
of database programming and am running into a problem: http://hashphp.org/pastebin.php?pid=567 The code works fine for the first instantiation of the DbTable (Product) class, but not the second. It seems clear that the reason it works the first time is that overload() isn't called until after describe() in DbTable's constructor, so DbTable can "see" its actual properties. The second time it's instantiated, the class is already overloaded, so it can't and describe() fails to populate the columns array. Unfortunately, seeing the problem is not helping me see the solution =P Does anyone have an idea for a workaround for this? BTW, the DbTable class isn't tied to any particular table, so it should be easy to test against any spare PostgreSQL tables you might have lying around (just change the test code). TIA, Cliff |
|
|||
|
Cliff Wells wrote:
> I'm trying to write a class for abstracting some aspects > of database programming and am running into a problem: > > http://hashphp.org/pastebin.php?pid=567 Well, I kept hacking at it and changing the describe() method from this: // get a description of a table function describe($table) { $db = $this->db; $q = $db->prepare("SELECT * FROM $table LIMIT 0"); $result = $db->execute($q); foreach ($result->tableInfo() as $c) $this->columns[$c['name']] = NULL; } to this: // get a description of a table function describe($table) { $db = $this->db; $columns = array(); $q = $db->prepare("SELECT * FROM $table LIMIT 0"); $result = $db->execute($q); foreach ($result->tableInfo() as $c) $columns[$c['name']] = NULL; $this->columns = $columns; } seems to fix it. I can't see any reason why other than what appears to be general PHP brokenness. If anyone has a rational explanation as to why this is so I'd be grateful to hear it. Regards, Cliff |