__asType()

php.internals

Sterling Hughes

23 years ago
Hi, When developing my SimpleXML extension, I've come to a situation where I want to have the ability (internally), to convert my object to a base type, when requested. I need this when both attributes and xml tags are identical within the same context, ie: data.xml <?xml version="1.0"?> <name id="20"> <id type="age">30</id> </name> name.php <?php $n = new Simple_XML('data.xml'); echo $n->id; ?> I'd like the ability to provide two modes of access for the object, array and object. So, one could do: $n['id']; // attr And $n->id; // node However, barring that, I'd like the ability to simply cast the object as a base type, essentially :: echo $n->id; echo $n->id->get_attr('type'); And have them both work. Are either of these a possibility? -Sterling
-- "Reductionists like to take things apart. The rest of us are just trying to get it together." - Larry Wall, Programming Perl, 3rd Edition

Brad LaFountain

23 years ago
Yes both of these are possible, Z2 only. Marcus has provided an example how to overried the opcodes from z2 and use them effectivly. He overrides 3 opcodes to get the array functionality. taken from php_spl.c ZEND_EXECUTE_HOOK(ZEND_FETCH_DIM_R); taken from spl_array.c.c ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_R) { if (cur_object_is_simple_xml()) { return_attribute_from_object(); NEXT_OPCODE(); } ZEND_EXECUTE_HOOK_ORIGINAL(ZEND_FETCH_DIM_R); } if you look what ZEND_EXECUTE_HOOK does it replaces zends opcodes with its own handlers. Now the down side of this is if different extensions keep doing this over and over, it could slow down array handling. AND Your other type of access is simple object overloading or you can setup zvals before hand. if you use overloading it might look something like this //Imagine this was in c class Simple_XML_Node { function __get($name) { $this->$name = get_node_from_data($this->raw_xml, $name); return $this->$name; } function get_attr($attribute) { return $this->whatever; } } otherwise you would have already set up the xml attributes at xml parse time I hope you know what i mean. Talk a look at marcus spl extension, he did some neat stuff there with the opcom overloading. http://marcus-boerger.de/php/ext/spl/ - Brad --- Sterling Hughes <sterling@bumblebury.com> wrote:
> Hi, > > When developing my SimpleXML extension, I've come to a situation where I > want to have the ability (internally), to convert my object to a base > type, when requested. I need this when both attributes and xml tags are > identical within the same context, ie: > > data.xml > <?xml version="1.0"?> > <name id="20"> > <id type="age">30</id> > </name> > > name.php > <?php > $n = new Simple_XML('data.xml'); > echo $n->id; > ?> > > I'd like the ability to provide two modes of access for the object, > array and object. So, one could do: > > $n['id']; // attr > > And > > $n->id; // node > > However, barring that, I'd like the ability to simply cast the object as > a base type, essentially :: > > echo $n->id; > echo $n->id->get_attr('type'); > > And have them both work. Are either of these a possibility? > > -Sterling > > -- > "Reductionists like to take things apart. The rest of us are > just trying to get it together." > - Larry Wall, Programming Perl, 3rd Edition > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
__________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com

Sterling Hughes

23 years ago
On Mon, 2003-05-05 at 18:55, Brad LaFountain wrote:
> Yes both of these are possible, Z2 only. > Marcus has provided an example how to overried the opcodes from > z2 and use them effectivly. He overrides 3 opcodes to get the array > functionality. > > taken from php_spl.c > ZEND_EXECUTE_HOOK(ZEND_FETCH_DIM_R); > > taken from spl_array.c.c > ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_R) { > if (cur_object_is_simple_xml()) { > return_attribute_from_object(); > NEXT_OPCODE(); > } > ZEND_EXECUTE_HOOK_ORIGINAL(ZEND_FETCH_DIM_R); > } > > if you look what ZEND_EXECUTE_HOOK does it replaces zends opcodes with > its own handlers. > > Now the down side of this is if different extensions keep doing this over and > over, it could slow down array handling. >
Right, I understand how the engine works here. Extensions like SimpleXML should not be modifying opcodes, imho. Its not their job to change the language semantics.
> AND > > Your other type of access is simple object overloading or you can > setup zvals before hand. > > if you use overloading it might look something like this > > //Imagine this was in c > class Simple_XML_Node { > function __get($name) { > $this->$name = get_node_from_data($this->raw_xml, $name); > return $this->$name; > } > function get_attr($attribute) { > return $this->whatever; > } > } > > otherwise you would have already set up the xml attributes at xml parse time
Yeah, that was my other solution. The problem is that requires an extra level of indirection, in cases with: <foo> <bar id="sterling">bam</bar> </foo> echo $doc->bar; // can't be done, this is an object with attributes -Sterling
-- Good judgement comes from experience, and experience comes from bad judgement. - Fred Brooks

Brad LaFountain

23 years ago
--- Sterling Hughes <sterling@bumblebury.com> wrote:
> On Mon, 2003-05-05 at 18:55, Brad LaFountain wrote: > > Yes both of these are possible, Z2 only. > > Marcus has provided an example how to overried the opcodes from > > z2 and use them effectivly. He overrides 3 opcodes to get the array > > functionality. > > > > taken from php_spl.c > > ZEND_EXECUTE_HOOK(ZEND_FETCH_DIM_R); > > > > taken from spl_array.c.c > > ZEND_EXECUTE_HOOK_FUNCTION(ZEND_FETCH_DIM_R) { > > if (cur_object_is_simple_xml()) { > > return_attribute_from_object(); > > NEXT_OPCODE(); > > } > > ZEND_EXECUTE_HOOK_ORIGINAL(ZEND_FETCH_DIM_R); > > } > > > > if you look what ZEND_EXECUTE_HOOK does it replaces zends opcodes with > > its own handlers. > > > > Now the down side of this is if different extensions keep doing this over > and > > over, it could slow down array handling. > > > > Right, I understand how the engine works here. Extensions like > SimpleXML should not be modifying opcodes, imho. Its not their job to > change the language semantics.
Yeah but its necesarry if you want to implement what you were describing. Unless they beef up the object api with something like... class SimpleXML { function __array($index) { } }
> > AND > > > > Your other type of access is simple object overloading or you can > > setup zvals before hand. > > > > if you use overloading it might look something like this > > > > //Imagine this was in c > > class Simple_XML_Node { > > function __get($name) { > > $this->$name = get_node_from_data($this->raw_xml, $name); > > return $this->$name; > > } > > function get_attr($attribute) { > > return $this->whatever; > > } > > } > > > > otherwise you would have already set up the xml attributes at xml parse > time > > Yeah, that was my other solution. The problem is that requires an extra > level of indirection, in cases with: > > <foo> > <bar id="sterling">bam</bar> > </foo> > > echo $doc->bar; // can't be done, this is an object with attributes
Yeah I knew I was missing something that you were asking because I knew you understood what I was explaining. again, a possible soultion could be another internal class SimpleXML { function __tostring() { } } But where do we draw the line... might as well add opperator overloading while your at some of this... - Brad __________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com

Andrei Zmievski

23 years ago
Sterling, array indexers aren't implemented yet, but I hear they are coming. -Andrei http://www.gravitonic.com/ * Gun manufacturers don't make bad products, bad parents do. *