ext/overload API
| From: | Timm Friebe | Date: | Thu, 14 Nov 2002 13:09:22 +0000 |
| Subject: | ext/overload API | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-91029@lists.php.net to get a copy of this message | ||
Hello,
Andrei, as I can see, you're the author of ext/overload. As suggested
earlier, I've added a second parameter to sybase_fetch_object() which
allows users to pass an object to be filled with the results from the
fetched row (e.g. $article= sybase_fetch_object($q, new Article()); or
$article= sybase_fetch_object($q, 'Article')).
Well, if "Article" is an overloaded class, they'd probably expect that
their __set*-handlers are called when the object's properties are set.
Of course, object_and_properties_init() does not do this. But indeed,
it'd be quite nice if they would.
Thus, one would need the C functions call_get_handler and
call_set_handler to be declared not static but PHPAPI (is this right?)
and an additional method to test if a zend_class_entry is overloaded.
* http://sitten-polizei.de/php/overload.patch
would make it work. Maybe macros would be a better way?
* http://sitten-polizei.de/php/sybase_ct.patch
shows a sample usage (btw, is there any documentation on TSRM?)
maybe _set_object_prop should be moved to ext/overload as a
utiltiy function?
I came up with this since I use sybase resultsets within SOAP, clearly
needing to distinguish strings from datetime types, int, floats and so
on. My first idea was to introduce a function
sybase_bind_datatype(SYBASE_DATETIME, 'Date')
but why not make use of an already existing and powerful extension?
Maybe an example illustrates best what you'd be able to do:
Example
-------
<?php
class Date {
var $_u;
function Date($m) {
switch (gettype($m)) {
case 'string': $this->_u= strtotime($m); break;
case 'integer': $this->_u= $m; break;
}
}
// [...]
}
class Article {
var $article_id, $caption, $text, $lastchange;
function __set($k, $v) {
$this->$k= $v;
return TRUE;
}
function __get($k) {
return $this->$k;
}
function __set_lastchange($val) {
$this->lastchange= &new Date($val);
return TRUE;
}
// [...]
}
overload('Article');
// [...]
$q= sybase_query(
'select article_id, caption, text, lastchange from article',
$dbh
);
while ($article= sybase_fetch_object($q, 'Article')) {
var_dump($article);
}
// [...]
?>
Sample Output
-------------
object(article)(4) {
["article_id"]=>
int(6100)
["caption"]=>
string(7) "Caption"
["text"]=>
string(19) "This is the text:-)"
["lastchange"]=>
&object(date)(1) {
["_u"]=>
int(1025458800)
}
}
- Timm