Skip to content

Latest commit

 

History

History
153 lines (122 loc) · 7.47 KB

11.3.md

File metadata and controls

153 lines (122 loc) · 7.47 KB

11.3 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_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_bool silent, zval *rv) ;//php7

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

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

  • 0: 如果属性不存在,则抛出一个notice错误。
  • 1: 如果属性不存在,不报错。
如果所查的属性不存在,那么此函数将返回IS_NULL类型的zval。 ### 更新对象的属性 ````c ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, char *name, int name_length, zval *value TSRMLS_DC); ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value);//php7

ZEND_API int zend_update_static_property(zend_class_entry *scope, char *name, int name_length, zval *value TSRMLS_DC); ZEND_API int zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value);//php7

zend_update_property用来更新对象的属性,zend_update_static_property用来更新类的静态属性。如果对象或者类中没有相关的属性,函数将自动的添加上。
### 读写对象与类属性的实例
假设我们已经在扩展中定义好下面的类:
````php
class baby
{
	public $age;
	public static $area;
	
	public function __construct($age, $area)
	{
		$this->age = $age;
		self::$area = $area;
		
		var_dump($this->age, self::$area);
	}
}

PHP_METHOD(baby, __construct)
{
	zval *age, *area, rv;
	zend_class_entry *ce;
	ce = Z_OBJCE_P(getThis());
	
	if( zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &age, &area) == FAILURE )
	{
		printf("Error\n");
		RETURN_NULL();
	}
	zend_update_property(ce, getThis(), "age", sizeof("age")-1, age);
	zend_update_static_property(ce, "area", sizeof("area")-1, area );
	
	age = NULL;
	area = NULL;
	
	age = zend_read_property(ce, getThis(), "age", sizeof("age")-1, 0, &rv );
	php_var_dump(age, 1);
	
	area = zend_read_static_property(ce, "area", sizeof("area")-1, 0, &rv );
	php_var_dump(area, 1);
	
}

//(php7)
PHP_METHOD(baby, __construct)
{
	zval *age, *area;
	zend_class_entry *ce;
	ce = Z_OBJCE_P(getThis());
	zval rv;

	if( zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &age, &area) == FAILURE )
	{
		php_printf("Error\n");
		RETURN_NULL();
	}
	zend_update_property(ce, getThis(), "age", sizeof("age")-1, age );
	zend_update_static_property(ce, "area", sizeof("area")-1, area );

	age = NULL;
	area = NULL;

	age = zend_read_property(ce, getThis(), "age", sizeof("age")-1, 0, &rv );
	php_var_dump(&age, 1 );

	area = zend_read_static_property(ce, "area", sizeof("area")-1, 0 );
	php_var_dump(&area, 1);
	
}
感谢 [@看你取的破名](http://weibo.com/taokuizu) 发现的错误。
### 一些其它的快捷函数 #### 更新对象与类的属性 ````c 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);

(php7)

````c
ZEND_API void zend_update_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zval *value);
ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value);
ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, const char *name, size_t name_length);
ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value);
ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value);
ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, const char *name, size_t name_length, double value);
ZEND_API void zend_update_property_str(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_string *value);
ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value);
ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value, size_t value_length);
ZEND_API void zend_unset_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length);

ZEND_API int zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value);
ZEND_API int zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length);
ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value);
ZEND_API int zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value);
ZEND_API int zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value);
ZEND_API int zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value);
ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_length);

links