类(Classes)¶

Phalcon7 框架主要是由类组成,而类由命名空间统一组织。本文档将教你如何实现一个类及其方法。

注册类(Registering Classes)¶

在 ext 目录增加 auth.h 文件,注册 Phalcon\Auth 类:

  1. #ifndef PHALCON_AUTH_H
  2. #define PHALCON_AUTH_H
  3.  
  4. #include "php_phalcon.h"
  5.  
  6. extern zend_class_entry *phalcon_auth_ce;
  7.  
  8. PHALCON_INIT_CLASS(Phalcon_Auth);
  9.  
  10. #endif /* PHALCON_AUTH_H */

注册方法和它的参数(Registering methods and its arguments)¶

然后,我们创建 auth.c 文件,在里面添加方法的原型:

  1. PHP_METHOD(Phalcon_Auth, __construct);
  2. PHP_METHOD(Phalcon_Auth, getIdentity);
  3. PHP_METHOD(Phalcon_Auth, auth);

之后我们为每个方法添加参数信息,在这里我们为类的构造方法添加参数信息:

  1. ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_auth__construct, 0, 0, 2)
  2. ZEND_ARG_INFO(0, adapter)
  3. ZEND_ARG_ARRAY_INFO(0, options, 0)
  4. ZEND_END_ARG_INFO()

现在,我们将所有方法赋予指定的类:

  1. static const zend_function_entry phalcon_auth_method_entry[] = {
  2. PHP_ME(Phalcon_Auth, __construct, arginfo_phalcon_auth__construct, ZEND_ACC_PUBLIC)
  3. PHP_ME(Phalcon_Auth, getIdentity, NULL, ZEND_ACC_PUBLIC)
  4. PHP_ME(Phalcon_Auth, auth, NULL, ZEND_ACC_PUBLIC)
  5. PHP_FE_END
  6. };

定义方法(Implementing a method)¶

仍然在 auth.c 文件中,我们添加代码:

  1. // PHP_METHOD(class_name, method_name)
  2. PHP_METHOD(Phalcon_Auth, __construct){
  3.  
  4. zval *adapter_name, *options = NULL;
  5.  
  6. // Receive the method parameters
  7. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &adapter_name, &options) == FAILURE) {
  8. RETURN_NULL();
  9. }
  10. }

With the above code we create the constructor of the class Phalcon\Auth, a method is defined with the macroPHPMETHOD, first we put the class name and then the name of the method, although Phalcon uses namespaces,class names have instead of \:

  1. PHP_METHOD(Phalcon_Auth, __construct){

If the method has parameters we receive them using zend_parse_parameters:

  1. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &adapter_name, &options) == FAILURE) {
  2. RETURN_NULL();
  3. }

If we do not receive the correct number of parameters will result in an error message. You see, there’s an argument“zz” to receive the parameters, this indicates the type of data received and the number of them. In the above examplethat means that the method is receiving two parameters. If they were three zval then it should be “zzz”.

Then the variables are received in respective order: &adapter_name, &options

原文: http://www.myleftstudio.com/internals/classes.html