操作对象(Objects Manipulation)

针对对象属性的操作,Phalcon API 在 Zend API 基础上封装了更多的函数(kernel/object.h),通过传递标志位(flag)自动完引用计数的处理。

标志位(flag)

标志位值描述
PH_NOISY值不存在时发出警告
PH_READONLY不增加引用计数
PH_COPY增加引用计数

常用函数列表

函数名
phalcon_isset_property
phalcon_read_property
phalcon_update_property

创建与实例化(Creation/Instantiation)

实例化框架中的类很容易:

  1. // Instantiate a object from the Phalcon\Mvc\Router\Route class entry
  2. object_init_ex(&route, phalcon_mvc_router_route_ce);
  3.  
  4. // Calling the constructor and passing a pattern as parameter
  5. ZVAL_STRING(&pattern, "#^/([a-zA-Z0-9\\_]+)[/]{0,1}$#", 1);
  6.  
  7. PHALCON_CALL_METHOD(&route, "__construct", &pattern);

The above code is the same as doing in PHP:

  1. <?php
  2.  
  3. $route = new Phalcon\Mvc\Router\Route("#^/([a-zA-Z0-9\\_]+)[/]{0,1}$#");

Moreover, if is not a Phalcon class then objects must then initialized as follows:

  1. zend_class_entry *reflection_ce;
  2.  
  3. // Obtain the ReflectionClass class entry, this will also call autoloaders
  4. reflection_ce = zend_fetch_class(SL("ReflectionClass"), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
  5.  
  6. // Instantiate the Reflection object
  7. object_init_ex(&reflection, reflection_ce);
  8.  
  9. // Pass a class name as constructor's parameter
  10. PHALCON_CALL_METHOD(NULL, reflection, "__construct", &class_name);

读写属性(Reading/Writing Properties)

Writing scalar values:

  1. // Create a stdClass object
  2. object_init(&employee);
  3.  
  4. // $employee->name = "Sonny"
  5. phalcon_update_property_string(&employee, SL("name"), "Sonny");
  6.  
  7. // $employee->age = 23
  8. phalcon_update_property_long(&employee, SL("age"), 23);
  9.  
  10. // Read the "name" property $name = $employee->name
  11. phalcon_read_property(&name, &employee, SL("name"), PH_NOISY_CC);

Assigning other zvals to properties:

  1. ZVAL_STRING(&language, "English");
  2.  
  3. object_init(&employee);
  4.  
  5. // $employee->language = $language
  6. phalcon_update_property_zval(&employee, SL("language"), &language);

Reading/Writing dynamical properties:

  1. ZVAL_STRING(&language, "English");
  2.  
  3. ZVAL_STRING(&property, "language");
  4.  
  5. object_init(&employee);
  6.  
  7. // $employee->$property = $language
  8. phalcon_update_property_zval_zval(&employee, &property, &language);
  9.  
  10. // $user_language = $employee->$property
  11. phalcon_read_property_zval(&user_language, &employee, &property, PH_NOISY_CC);

Reading/Writing static properties:

  1. // Updating a static member with a string zval
  2. ZVAL_STRING(&greeting, "hello world");
  3. phalcon_update_static_property(SL("phalcon\\some\\component"), SL("_someString"), &greeting);
  4.  
  5. // Updating a static member with a long zval
  6. PHALCON_INIT_VAR(number);
  7. ZVAL_LONG(number, 150);
  8. phalcon_update_static_property(SL("phalcon\\some\\component"), SL("_someInteger"), &number);
  9.  
  10. // Reading a static member
  11. phalcon_read_static_property(&number, SL("phalcon\\some\\component"), SL("_someInteger"));