On Fri, Feb 20, 2009 at 12:31 PM, Matthew C. Rice <mr...@rcs.us> 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

Reply via email to