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
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--
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com