FAQ
Hello everyone,


I am building a custom PHP Extension. It is object based, and while
the documentation seems to be lacking a little on this aspect in
extensions, I haven't let that slow me down. I have used other
extensions as examples, and done a great deal of searching around to
find everything I have needed until now.

I currently have the following http://pastebin.com/m74c98b43 ( the
start method ) in my classes __construct ( I actually use PHP_MALIAS to
alias __construct to the function ) .. This works perfect. It creates a
array in the object, just as I expect it to.

I can even print out the number of elements and their values in the
debug function by passing $Obj->Property to it

The problem I am having is that I can't seem to figure out a way to
append values on to the ZVal Array in another PHP_METHOD(). I have tried
combinations of zend_hash_find() && zend_hash_update() to no avail. Is
there anyone that might be able to point me in the right direction as to
how to do this?



Matthew C. Rice

Search Discussions

  • Nathan Nobbe at Feb 20, 2009 at 9:42 pm

    On Fri, Feb 20, 2009 at 12:31 PM, Matthew C. Rice wrote:

    Hello everyone,


    I am building a custom PHP Extension. It is object based, and while the
    documentation seems to be lacking a little on this aspect in extensions, I
    haven't let that slow me down. I have used other extensions as examples, and
    done a great deal of searching around to find everything I have needed until
    now.

    I currently have the following http://pastebin.com/m74c98b43 ( the
    start method ) in my classes __construct ( I actually use PHP_MALIAS to
    alias __construct to the function ) .. This works perfect. It creates a
    array in the object, just as I expect it to.

    I can even print out the number of elements and their values in the
    debug function by passing $Obj->Property to it

    The problem I am having is that I can't seem to figure out a way to
    append values on to the ZVal Array in another PHP_METHOD(). I have tried
    combinations of zend_hash_find() && zend_hash_update() to no avail. Is
    there anyone that might be able to point me in the right direction as to how
    to do this?

    i think mainly you just need to read the zval from the instance, operate on
    it, then update the instance. heres some working code from the extension
    ive been working on,

    /* {{{ proto public void MacroCommand::addSubCommand(string commandClassRef)
    add a subcommand to this MacroCommand */
    PHP_METHOD(MacroCommand, addSubCommand)
    {
    zval *this, *subCommands, *subCommand;
    char *rawSubCommand;
    int rawSubCommandLength;

    if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
    &rawSubCommand, &rawSubCommandLength) == FAILURE) {
    RETURN_NULL();
    }

    MAKE_STD_ZVAL(subCommand);
    ZVAL_STRINGL(subCommand, rawSubCommand, rawSubCommandLength, 1);

    this = getThis();
    zend_class_entry *this_ce = zend_get_class_entry(this);
    subCommands = zend_read_property(puremvc_macrocommand_ce, this,
    "subCommands",
    sizeof("subCommands")-1, 1 TSRMLS_CC);

    ZVAL_ADDREF(subCommand);
    add_next_index_zval(subCommands, subCommand);

    zend_update_property(puremvc_macrocommand_ce, this, "subCommands",
    sizeof("subCommands")-1,
    subCommands TSRMLS_CC);
    }
    /* }}} */

    and heres my unit test for this method,

    --TEST--
    MacroCommand::addSubCommand(), ensure addSubCommand actually stores the
    ICommand in an internal array
    --SKIPIF--
    <?php if (!extension_loaded("pure_mvc")) print "skip"; ?>
    --FILE--
    <?php
    class SubCommand {}
    class MyMacroCommand extends MacroCommand {
    protected function initializeMacroCommand() {
    $this->addSubCommand('SubCommand');
    }
    }
    $macroCmd = new MyMacroCommand();
    var_dump($macroCmd);
    ?>
    --EXPECT--
    object(MyMacroCommand)#1 (2) {
    ["facade:protected"]=>
    string(0) ""
    ["subCommands:private"]=>
    array(1) {
    [0]=>
    string(10) "SubCommand"
    }
    }

    hope this helps,

    -nathan
  • Matthew C. Rice at Feb 20, 2009 at 10:02 pm
    Nathan,

    Thanks a bunch. That was exactly what I was looking for. Is there
    any place you would consider a good resource to learn/get documentation
    for these functions?


    Matthew C. Rice

    Nathan Nobbe wrote:
    On Fri, Feb 20, 2009 at 12:31 PM, Matthew C. Rice wrote:

    Hello everyone,


    I am building a custom PHP Extension. It is object based, and while the
    documentation seems to be lacking a little on this aspect in extensions, I
    haven't let that slow me down. I have used other extensions as examples, and
    done a great deal of searching around to find everything I have needed until
    now.

    I currently have the following http://pastebin.com/m74c98b43 ( the
    start method ) in my classes __construct ( I actually use PHP_MALIAS to
    alias __construct to the function ) .. This works perfect. It creates a
    array in the object, just as I expect it to.

    I can even print out the number of elements and their values in the
    debug function by passing $Obj->Property to it

    The problem I am having is that I can't seem to figure out a way to
    append values on to the ZVal Array in another PHP_METHOD(). I have tried
    combinations of zend_hash_find() && zend_hash_update() to no avail. Is
    there anyone that might be able to point me in the right direction as to how
    to do this?

    i think mainly you just need to read the zval from the instance, operate on
    it, then update the instance. heres some working code from the extension
    ive been working on,

    /* {{{ proto public void MacroCommand::addSubCommand(string commandClassRef)
    add a subcommand to this MacroCommand */
    PHP_METHOD(MacroCommand, addSubCommand)
    {
    zval *this, *subCommands, *subCommand;
    char *rawSubCommand;
    int rawSubCommandLength;

    if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
    &rawSubCommand, &rawSubCommandLength) == FAILURE) {
    RETURN_NULL();
    }

    MAKE_STD_ZVAL(subCommand);
    ZVAL_STRINGL(subCommand, rawSubCommand, rawSubCommandLength, 1);

    this = getThis();
    zend_class_entry *this_ce = zend_get_class_entry(this);
    subCommands = zend_read_property(puremvc_macrocommand_ce, this,
    "subCommands",
    sizeof("subCommands")-1, 1 TSRMLS_CC);

    ZVAL_ADDREF(subCommand);
    add_next_index_zval(subCommands, subCommand);

    zend_update_property(puremvc_macrocommand_ce, this, "subCommands",
    sizeof("subCommands")-1,
    subCommands TSRMLS_CC);
    }
    /* }}} */

    and heres my unit test for this method,

    --TEST--
    MacroCommand::addSubCommand(), ensure addSubCommand actually stores the
    ICommand in an internal array
    --SKIPIF--
    <?php if (!extension_loaded("pure_mvc")) print "skip"; ?>
    --FILE--
    <?php
    class SubCommand {}
    class MyMacroCommand extends MacroCommand {
    protected function initializeMacroCommand() {
    $this->addSubCommand('SubCommand');
    }
    }
    $macroCmd = new MyMacroCommand();
    var_dump($macroCmd);
    ?>
    --EXPECT--
    object(MyMacroCommand)#1 (2) {
    ["facade:protected"]=>
    string(0) ""
    ["subCommands:private"]=>
    array(1) {
    [0]=>
    string(10) "SubCommand"
    }
    }

    hope this helps,

    -nathan
  • Nathan Nobbe at Feb 20, 2009 at 10:47 pm

    On Fri, Feb 20, 2009 at 3:02 PM, Matthew C. Rice wrote:

    Nathan,

    Thanks a bunch. That was exactly what I was looking for.

    word

    Is there any place you would consider a good resource to learn/get
    documentation for these functions?

    my secret weapon,

    http://www.amazon.com/Extending-Embedding-PHP-Developers-Library/dp/067232704X

    :D

    but youll still have to do all the things youre doing now. digging through
    other extensions, looking through the code under the Zend directory and so
    forth.. the book just makes things *way* easier to get the ball rolling.

    -nathan
  • Matthew C. Rice at Feb 20, 2009 at 10:52 pm
    Nathan,


    Thanks one last time. I am ordering the book now. I was going to
    originally, but read a bit on some reviews and it seemed it was missing
    some key Object parts. Most of what I have learned so far was based on
    other extensions, grep'ing the actual Zend source, and the post Sara
    Golemon had here, http://devzone.zend.com/node/view/id/1024 , which
    unfortunately never makes it to the "Part IV" on Objects it notes was
    coming up ( a few years ago now ).

    Never the less, thanks again!


    Matthew C. Rice



    Nathan Nobbe wrote:
    On Fri, Feb 20, 2009 at 3:02 PM, Matthew C. Rice wrote:

    Nathan,

    Thanks a bunch. That was exactly what I was looking for.

    word

    Is there any place you would consider a good resource to learn/get
    documentation for these functions?

    my secret weapon,

    http://www.amazon.com/Extending-Embedding-PHP-Developers-Library/dp/067232704X

    :D

    but youll still have to do all the things youre doing now. digging through
    other extensions, looking through the code under the Zend directory and so
    forth.. the book just makes things *way* easier to get the ball rolling.

    -nathan
  • Marcus Boerger at Feb 21, 2009 at 12:19 pm
    Hello Matthew,

    I gave a bunch of tutorials on this with various other folks, Johannes,
    Sara, Wez. The slides from November 2007 are still up and contain probably
    more information than you ever wanted to know. There is also the demo
    extension that you can access through cvs.

    http://talks.somabo.de/200711_php_code_camp.pdf
    http://talks.somabo.de/200711_php_code_camp.pps
    http://cvs.php.net/viewvc.cgi/pecl/demoext/

    Friday, February 20, 2009, 11:02:14 PM, you wrote:
    Nathan,
    Thanks a bunch. That was exactly what I was looking for. Is there
    any place you would consider a good resource to learn/get documentation
    for these functions?
    Matthew C. Rice
    Nathan Nobbe wrote:
    On Fri, Feb 20, 2009 at 12:31 PM, Matthew C. Rice wrote:

    Hello everyone,


    I am building a custom PHP Extension. It is object based, and while the
    documentation seems to be lacking a little on this aspect in extensions, I
    haven't let that slow me down. I have used other extensions as examples, and
    done a great deal of searching around to find everything I have needed until
    now.

    I currently have the following http://pastebin.com/m74c98b43 ( the
    start method ) in my classes __construct ( I actually use PHP_MALIAS to
    alias __construct to the function ) .. This works perfect. It creates a
    array in the object, just as I expect it to.

    I can even print out the number of elements and their values in the
    debug function by passing $Obj->Property to it

    The problem I am having is that I can't seem to figure out a way to
    append values on to the ZVal Array in another PHP_METHOD(). I have tried
    combinations of zend_hash_find() && zend_hash_update() to no avail. Is
    there anyone that might be able to point me in the right direction as to how
    to do this?

    i think mainly you just need to read the zval from the instance, operate on
    it, then update the instance. heres some working code from the extension
    ive been working on,

    /* {{{ proto public void MacroCommand::addSubCommand(string commandClassRef)
    add a subcommand to this MacroCommand */
    PHP_METHOD(MacroCommand, addSubCommand)
    {
    zval *this, *subCommands, *subCommand;
    char *rawSubCommand;
    int rawSubCommandLength;

    if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
    &rawSubCommand, &rawSubCommandLength) == FAILURE) {
    RETURN_NULL();
    }

    MAKE_STD_ZVAL(subCommand);
    ZVAL_STRINGL(subCommand, rawSubCommand, rawSubCommandLength, 1);

    this = getThis();
    zend_class_entry *this_ce = zend_get_class_entry(this);
    subCommands = zend_read_property(puremvc_macrocommand_ce, this,
    "subCommands",
    sizeof("subCommands")-1, 1 TSRMLS_CC);

    ZVAL_ADDREF(subCommand);
    add_next_index_zval(subCommands, subCommand);

    zend_update_property(puremvc_macrocommand_ce, this, "subCommands",
    sizeof("subCommands")-1,
    subCommands TSRMLS_CC);
    }
    /* }}} */

    and heres my unit test for this method,

    --TEST--
    MacroCommand::addSubCommand(), ensure addSubCommand actually stores the
    ICommand in an internal array
    --SKIPIF--
    <?php if (!extension_loaded("pure_mvc")) print "skip"; ?>
    --FILE--
    <?php
    class SubCommand {}
    class MyMacroCommand extends MacroCommand {
    protected function initializeMacroCommand() {
    $this->addSubCommand('SubCommand');
    }
    }
    $macroCmd = new MyMacroCommand();
    var_dump($macroCmd);
    ?>
    --EXPECT--
    object(MyMacroCommand)#1 (2) {
    ["facade:protected"]=>
    string(0) ""
    ["subCommands:private"]=>
    array(1) {
    [0]=>
    string(10) "SubCommand"
    }
    }

    hope this helps,

    -nathan



    Best regards,
    Marcus
  • Matthew C. Rice at Feb 21, 2009 at 1:52 pm
    Marcus,


    Thanks a bunch! I had a similar style pdf I found on the pecl site
    titled 200510_zend_conf_php_extension_development.pdf , but it was
    lacking a little on OOP. This looks like exactly what I needed. Between
    this and the Sara Golemon book I hope to fill a major gap.. Thanks again !!!


    Matthew C. Rice

    Marcus Boerger wrote:
    Hello Matthew,

    I gave a bunch of tutorials on this with various other folks, Johannes,
    Sara, Wez. The slides from November 2007 are still up and contain probably
    more information than you ever wanted to know. There is also the demo
    extension that you can access through cvs.

    http://talks.somabo.de/200711_php_code_camp.pdf
    http://talks.somabo.de/200711_php_code_camp.pps
    http://cvs.php.net/viewvc.cgi/pecl/demoext/

    Friday, February 20, 2009, 11:02:14 PM, you wrote:
    Nathan,
    Thanks a bunch. That was exactly what I was looking for. Is there
    any place you would consider a good resource to learn/get documentation
    for these functions?
    Matthew C. Rice
    Nathan Nobbe wrote:
    On Fri, Feb 20, 2009 at 12:31 PM, Matthew C. Rice wrote:

    Hello everyone,


    I am building a custom PHP Extension. It is object based, and while the
    documentation seems to be lacking a little on this aspect in extensions, I
    haven't let that slow me down. I have used other extensions as examples, and
    done a great deal of searching around to find everything I have needed until
    now.

    I currently have the following http://pastebin.com/m74c98b43 ( the
    start method ) in my classes __construct ( I actually use PHP_MALIAS to
    alias __construct to the function ) .. This works perfect. It creates a
    array in the object, just as I expect it to.

    I can even print out the number of elements and their values in the
    debug function by passing $Obj->Property to it

    The problem I am having is that I can't seem to figure out a way to
    append values on to the ZVal Array in another PHP_METHOD(). I have tried
    combinations of zend_hash_find() && zend_hash_update() to no avail. Is
    there anyone that might be able to point me in the right direction as to how
    to do this?
    i think mainly you just need to read the zval from the instance, operate on
    it, then update the instance. heres some working code from the extension
    ive been working on,

    /* {{{ proto public void MacroCommand::addSubCommand(string commandClassRef)
    add a subcommand to this MacroCommand */
    PHP_METHOD(MacroCommand, addSubCommand)
    {
    zval *this, *subCommands, *subCommand;
    char *rawSubCommand;
    int rawSubCommandLength;

    if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
    &rawSubCommand, &rawSubCommandLength) == FAILURE) {
    RETURN_NULL();
    }

    MAKE_STD_ZVAL(subCommand);
    ZVAL_STRINGL(subCommand, rawSubCommand, rawSubCommandLength, 1);

    this = getThis();
    zend_class_entry *this_ce = zend_get_class_entry(this);
    subCommands = zend_read_property(puremvc_macrocommand_ce, this,
    "subCommands",
    sizeof("subCommands")-1, 1 TSRMLS_CC);

    ZVAL_ADDREF(subCommand);
    add_next_index_zval(subCommands, subCommand);

    zend_update_property(puremvc_macrocommand_ce, this, "subCommands",
    sizeof("subCommands")-1,
    subCommands TSRMLS_CC);
    }
    /* }}} */

    and heres my unit test for this method,

    --TEST--
    MacroCommand::addSubCommand(), ensure addSubCommand actually stores the
    ICommand in an internal array
    --SKIPIF--
    <?php if (!extension_loaded("pure_mvc")) print "skip"; ?>
    --FILE--
    <?php
    class SubCommand {}
    class MyMacroCommand extends MacroCommand {
    protected function initializeMacroCommand() {
    $this->addSubCommand('SubCommand');
    }
    }
    $macroCmd = new MyMacroCommand();
    var_dump($macroCmd);
    ?>
    --EXPECT--
    object(MyMacroCommand)#1 (2) {
    ["facade:protected"]=>
    string(0) ""
    ["subCommands:private"]=>
    array(1) {
    [0]=>
    string(10) "SubCommand"
    }
    }

    hope this helps,

    -nathan



    Best regards,
    Marcus

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedFeb 20, '09 at 8:00p
activeFeb 21, '09 at 1:52p
posts7
users3
websitephp.net

People

Translate

site design / logo © 2022 Grokbase