php对象属性读写

读取对象的属性
ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, char *name, int name_length, zend_bool silent TSRMLS_DC);

ZEND_API zval *zend_read_static_property(zend_class_entry *scope, char *name, int name_length, zend_bool silent TSRMLS_DC);

zend_read_property函数用于读取对象的属性,而zend_read_static_property则用于读取静态属性。可以看出,静态属性是直接保存在类上的,用具体的对象无关。

silent参数:

  • 0: 如果属性不存在,则抛出一个notice错误。
  • 1: 如果属性不存在,不报错。

如果所查的属性不存在,那么此函数将返回IS_NULL类型的zval。

更新对象的属性:

ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, char *name, int name_length, zval *value TSRMLS_DC);
ZEND_API int zend_update_static_property(zend_class_entry *scope, char *name, int name_length, zval *value TSRMLS_DC);

zend_update_property用来更新对象的属性,zend_update_static_property用来更新类的静态属性。如果对象或者类中没有相关的属性,函数将自动的添加上。

读写对象与类属性的实例

假设我们已经在扩展中定义好下面的类:

class baby
{
	public $age;
	public static $area;
	
	public function __construct($age, $area)
	{
		$this->age = $age;
		self::$area = $area;
		
		var_dump($this->age, self::$area);
	}
}

ZEND_METHOD(baby, __construct)
{
	zval *age, *area;
	zend_class_entry *ce;
	ce = Z_OBJCE_P(getThis());
	if( zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &age, &area) == FAILURE )
	{
		printf("Error\n");
		RETURN_NULL();
	}
	zend_update_property(ce, getThis(), "age", sizeof("age")-1, age TSRMLS_CC);
	zend_update_static_property(ce, "area", sizeof("area")-1, area TSRMLS_CC);
	
	age = NULL;
	area = NULL;
	
	age = zend_read_property(ce, getThis(), "age", sizeof("age")-1, 0 TSRMLS_DC);
	php_var_dump(&age, 1 TSRMLS_CC);
	
	area = zend_read_static_property(ce, "area", sizeof("area")-1, 0 TSRMLS_DC);
	php_var_dump(&area, 1 TSRMLS_CC);
	
}
一些其它的快捷函数
ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, char *name, int name_length TSRMLS_DC);
ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, char *name, int name_length, long value TSRMLS_DC);
ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, char *name, int name_length, long value TSRMLS_DC);
ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, char *name, int name_length, double value TSRMLS_DC);
ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, char *name, int name_length, const char *value TSRMLS_DC);
ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zval *object, char *name, int name_length, const char *value, int value_length TSRMLS_DC);


ZEND_API int zend_update_static_property_null(zend_class_entry *scope, char *name, int name_length TSRMLS_DC);
ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, char *name, int name_length, long value TSRMLS_DC);
ZEND_API int zend_update_static_property_long(zend_class_entry *scope, char *name, int name_length, long value TSRMLS_DC);
ZEND_API int zend_update_static_property_double(zend_class_entry *scope, char *name, int name_length, double value TSRMLS_DC);
ZEND_API int zend_update_static_property_string(zend_class_entry *scope, char *name, int name_length, const char *value TSRMLS_DC);
ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, char *name, int name_length, const char *value, int value_length TSRMLS_DC);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值