# How to add new functions to SimpleXML extention

- Thread: https://externals.io/message/6996
- Original archive: https://news-web.php.net/php.internals/6996
- Started by Alexander on 2004-01-12T15:28:05+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #6996 — Alexander — 2004-01-12T15:28:05+00:00

URL: https://externals.io/message/6996#6996

Hi!

I'm just playing with simplexml and wonder if two new functions of the
simplexml_element object would be usefull: getName() and getParent(). I'm
interesting in adding them to the extention, if possible. What should I do
for this? (I also think that it may be in FAQ, but I did not find that info
with my search skills).
Basically, this is my first experiment with PHP Extensions. I may be wrong
with my code, but tests pass is ok

-----------------------
Thank you for time,
Alexander Netkachev
http://devlink.narod.ru/

Code for getName function:
/* {{{ getName()
 */
SXE_METHOD(getName)
{
    char *name;
    xmlNodePtr node; // current node
  php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
    GET_NODE(sxe, node);
    if (node && (name = (char *)(node->name))) {
        RETURN_STRING(name, 1);
    }
    RETURN_EMPTY_STRING();
}
/* }}} */

/* {{{ getParent()
 */
SXE_METHOD(getParent)
{
    xmlNodePtr node; // current node
    xmlNodePtr pNode; // node->parent
  php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
    GET_NODE(sxe, node);
    if (node && (pNode = node->parent)) {
        _node_as_zval(sxe, pNode, return_value TSRMLS_CC);
        return;
    }
    RETURN_NULL();
}
/* }}} */

File: ext/simplexml/tests/019.phpt
--TEST--
SimpleXML: Test getParent() and getName() functions
--SKIPIF--
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
--FILE--
<?php
$doc = simplexml_load_string('<html><body></body></html>');
var_dump(trim($doc->body->getParent()->getName()));
echo '--done--';
?>
--EXPECT--
string(4) "html"
--done

---

## #6998 — Sterling Hughes — 2004-01-12T19:23:40+00:00

URL: https://externals.io/message/6996#6998 · In reply to: #6996

Funny, i writing a response to adam's message while this got to the
mailing list.  Take a look at my response to adam, as it covers this
patch as well.

-Sterling

> Hi!
> 
> I'm just playing with simplexml and wonder if two new functions of the
> simplexml_element object would be usefull: getName() and getParent(). I'm
> interesting in adding them to the extention, if possible. What should I do
> for this? (I also think that it may be in FAQ, but I did not find that info
> with my search skills).
> Basically, this is my first experiment with PHP Extensions. I may be wrong
> with my code, but tests pass is ok
> 
> -----------------------
> Thank you for time,
> Alexander Netkachev
> http://devlink.narod.ru/
> 
> Code for getName function:
> /* {{{ getName()
>  */
> SXE_METHOD(getName)
> {
>     char *name;
>     xmlNodePtr node; // current node
>   php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
>     GET_NODE(sxe, node);
>     if (node && (name = (char *)(node->name))) {
>         RETURN_STRING(name, 1);
>     }
>     RETURN_EMPTY_STRING();
> }
> /* }}} */
> 
> /* {{{ getParent()
>  */
> SXE_METHOD(getParent)
> {
>     xmlNodePtr node; // current node
>     xmlNodePtr pNode; // node->parent
>   php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
>     GET_NODE(sxe, node);
>     if (node && (pNode = node->parent)) {
>         _node_as_zval(sxe, pNode, return_value TSRMLS_CC);
>         return;
>     }
>     RETURN_NULL();
> }
> /* }}} */
> 
> File: ext/simplexml/tests/019.phpt
> --TEST--
> SimpleXML: Test getParent() and getName() functions
> --SKIPIF--
> <?php if (!extension_loaded("simplexml")) print "skip"; ?>
> --FILE--
> <?php
> $doc = simplexml_load_string('<html><body></body></html>');
> var_dump(trim($doc->body->getParent()->getName()));
> echo '--done--';
> ?>
> --EXPECT--
> string(4) "html"
> --done

---

## #6999 — Marcus Boerger — 2004-01-12T19:29:02+00:00

URL: https://externals.io/message/6996#6999 · In reply to: #6996

Hello Alexander,

generally a good idea but we are in feature freeze mode for PHP 5.0.x which
is our current head. That means you have to suspend your idea until HEAD and
PHP_5_0 are different branches. This will most likely happen during the next
two months. To not forget your idea you can open a bug report and insert
your idea with category 'Feature/Change request'.

regards
marcus

Monday, January 12, 2004, 4:28:05 PM, you wrote:

> Hi!

> I'm just playing with simplexml and wonder if two new functions of the
> simplexml_element object would be usefull: getName() and getParent(). I'm
> interesting in adding them to the extention, if possible. What should I do
> for this? (I also think that it may be in FAQ, but I did not find that info
> with my search skills).
> Basically, this is my first experiment with PHP Extensions. I may be wrong
> with my code, but tests pass is ok

> -----------------------
> Thank you for time,
> Alexander Netkachev
> http://devlink.narod.ru/

> Code for getName function:
> /* {{{ getName()
>  */
> SXE_METHOD(getName)
> {
>     char *name;
>     xmlNodePtr node; // current node
>   php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
>     GET_NODE(sxe, node);
>     if (node && (name = (char *)(node->name))) {
>         RETURN_STRING(name, 1);
>     }
>     RETURN_EMPTY_STRING();
> }
> /* }}} */

> /* {{{ getParent()
>  */
> SXE_METHOD(getParent)
> {
>     xmlNodePtr node; // current node
>     xmlNodePtr pNode; // node->parent
>   php_sxe_object *sxe = php_sxe_fetch_object(getThis() TSRMLS_CC);
>     GET_NODE(sxe, node);
>     if (node && (pNode = node->parent)) {
>         _node_as_zval(sxe, pNode, return_value TSRMLS_CC);
>         return;
>     }
>     RETURN_NULL();
> }
> /* }}} */

> File: ext/simplexml/tests/019.phpt
> --TEST--
> SimpleXML: Test getParent() and getName() functions
> --SKIPIF--
> <?php if (!extension_loaded("simplexml")) print "skip"; ?>
> --FILE--
> <?php
> $doc = simplexml_load_string('<html><body></body></html>');
> var_dump(trim($doc->body->getParent()->getName()));
> echo '--done--';
?>>
> --EXPECT--
> string(4) "html"
> --done--




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #7016 — Alexander — 2004-01-12T22:30:41+00:00

URL: https://externals.io/message/6996#7016 · In reply to: #6998

Hi,

Hope, I understand your point. It looks like having two DOMs is not in your
plans :-) getParent is really equal to xsearch('..'). But what about name?
Is it possible to get the name of the current simplexml_element using
xsearch?
I also wonder if I created "right" functions because I plan to create a
tutor article "How to extend PHP" with these functions as examples.
Are they correct from this point of view?

Regards,
Alexander Netkachev

Sterling Hughes wrote:

> Funny, i writing a response to adam's message while this got to the
> mailing list.  Take a look at my response to adam, as it covers this
> patch as well.
> 
> -Sterling
> 
> > […]
