05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

* ... */<br />

};<br />

<strong>PHP</strong>_MINIT_FUNCTION(foo)<br />

{<br />

zend_class_entry foo_class_entry;<br />

INIT_CLASS_ENTRY(foo_class_entry, "my_class", php_foo_class_functions);<br />

foo_class_entry_ptr =<br />

zend_register_internal_class(&foo_class_entry TSRMLS_CC);<br />

/* ... */<br />

<strong>PHP</strong>_FUNCTION(my_object) {<br />

object_init_ex(return_value, foo_class_entry_ptr);<br />

add_property_long(return_value,"version",<br />

foo_remote_get_version(XG(session)));<br />

add_property_bool(...)<br />

add_property_string(...)<br />

add_property_stringl(...)<br />

...<br />

From the user space, you would then have:<br />

$obj = my_object( );<br />

$obj->add( );<br />

If instead you want traditional instantiation, like this:<br />

$obj = new my_class( );<br />

use the automatically initialized this_ptr instead of return_value:<br />

<strong>PHP</strong>_FUNCTION(my_class) {<br />

add_property_long(this_ptr, "version",<br />

foo_remote_get_version(XG(session)));<br />

add_property_bool(...)<br />

add_property_string(...)<br />

add_property_stringl(...)<br />

...<br />

You can access class properties from the various functions and methods like this:<br />

zval **tmp;<br />

if(zend_hash_find(HASH_OF(this_ptr), "my_property", 12,<br />

(void **)&tmp) == SUCCESS) {<br />

convert_to_string_ex(tmp);<br />

printf("my_property is set to %s\n", Z_STRVAL_PP(status));<br />

}<br />

You can set/update a class property as follows:<br />

add_property_string(this_ptr, "filename", fn, 1);<br />

add_property_stringl(this_ptr, "key", "value", 5, 1);<br />

add_property_bool(this_ptr, "toggle", setting?0:1);<br />

add_property_long(this_ptr, "length", 12345);<br />

add_property_double(this_ptr, "price", 19.95);<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Returning Values | 341

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!