search for in the  
<ArraysResources>
Last updated: Thu, 19 May 2005

Objects

Since objects can be converted to arrays (and vice versa), you might have already guessed that they have a lot of similarities to arrays in PHP. Objects are maintained with the same hash functions, but there's a different API for creating them.

To initialize an object, you use the function object_init():
zval *new_object;

MAKE_STD_ZVAL(new_object);

if(object_init(new_object) != SUCCESS)
{
    // do error handling here
}
You can use the functions described in Table 53-4 to add members to your object.

Table 53-4. Zend's API for Object Creation

FunctionDescription
add_property_long(zval *object, char *key, long l);()Adds a long to the object.
add_property_unset(zval *object, char *key);()Adds an unset property to the object.
add_property_bool(zval *object, char *key, int b);()Adds a Boolean to the object.
add_property_resource(zval *object, char *key, long r);()Adds a resource to the object.
add_property_double(zval *object, char *key, double d);()Adds a double to the object.
add_property_string(zval *object, char *key, char *str, int duplicate);()Adds a string to the object.
add_property_stringl(zval *object, char *key, char *str, uint length, int duplicate);()Adds a string of the specified length to the object. This function is faster than add_property_string() and also binary-safe.
add_property_zval(zval *obect, char *key, zval *container):() Adds a zval container to the object. This is useful if you have to add properties which aren't simple types like integers or strings but arrays or other objects.


User Contributed Notes
Objects
glamm at a-s-i dot com
07-Aug-2003 01:14
Regarding the note written by jo-shi at web dot de:

I do not think that the line
  ce = emalloc(sizeof(zend_class_entry));
is required, as the line
  ce = zend_register_internal_class(&class_entry TSRMLS_CC);
actually appears to go through the process of performing the memory allocation and reference counting of the object.
jo-shi at web dot de
03-Dec-2002 12:54
add_property_[*](*) overwrites existing values in your object:

ex:
 var_dump($myobj);
 object(objectname)(1){
   ["_myvalue"]=>int(23);
 }
 so in func1 i "add" a property again:
 PHP_FUNCTION(func1){
   add_property_long(getThis(),"_myvalue",32);
 }
 should (as expected) result
 object(objectname)(1){
   ["_myvalue"]=>int(32);
 }
/* annotation: getThis() is a function used by many extensions to find out the $this value in an object */
jo-shi at web dot de
03-Dec-2002 12:47
you can add functions to your object:
you may declare global
  static zend_function_entry
   php_my_class_functions[] = {
     ZEND_FE (func1, NULL)
     ZEND_FE (func2, NULL)
     {NULL, NULL, NULL}
   };

you may declare local
  zend_class_entry my_class_entry;
  zend_class_entry *ce;

you should do something like this
  INIT_CLASS_ENTRY(
   func_class_entry,
   "CLASSNAME",
   php_my_class_functions);
  ce = emalloc(sizeof(zend_class_entry));
  ce = zend_register_internal_class (
  &my_class_entry TSRMLS_CC);
  object_init_ex(return_value, ce);
this should produce a returned object in your php_code:
$myobj = yourfunc(/*yourparams*/);
$myobj->func1();
$myobj->func2();
k at ailis dot de
18-Mar-2002 10:32
object_init() and object_init_ex() are not calling the constructor of a class. You have to do this by yourself after creating the object.

<ArraysResources>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT