Hello World!

每种语言都有自己的 “Hello World!” 示例。 在 Zephir 中, 这个介绍性示例展示了该语言的一 些重要功能。

Zephir 中的代码必须放在类中。 该语言旨在创建面向对象的库框架, 因此不允许在类之外使用代码。 此外, 还需要命名空间:

  1. namespace Test;
  2. /**
  3. * This is a sample class
  4. */
  5. class Hello
  6. {
  7. /**
  8. * This is a sample method
  9. */
  10. public function say()
  11. {
  12. echo "Hello World!";
  13. }
  14. }

编译此类后, 它将生成以下代码, 该代码由 gcc/clang/vc ++ 透明地编译:

  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include "php.h"
  5. #include "php_test.h"
  6. #include "test.h"
  7. #include "kernel/main.h"
  8. /**
  9. * This is a sample class
  10. */
  11. ZEPHIR_INIT_CLASS(Test_Hello) {
  12. ZEPHIR_REGISTER_CLASS(Test, Hello, hello, test_hello_method_entry, 0);
  13. return SUCCESS;
  14. }
  15. /**
  16. * This is a sample method
  17. */
  18. PHP_METHOD(Test_Hello, say) {
  19. php_printf("%s", "Hello World!");
  20. }

实际上, 使用 Zephir 的开发人员必须知道甚至理解 c, 这是不希望的。但是, 如果您对编译器、php 内部或 c 语言本身有任何经验, 这将使您更清楚地了解在使用 Zephir 时内部发生的情况。