Zephir-(12) php functions and exception handling-php Tutorial

Source: Internet
Author: User
Zephir-(12) php functions and exception handling
# Zephir-php functions and exception handling #! [] ( http://i.imgur.com/OuZmZ0R.png ) ## Preface ## *** first, I would like to thank all zephir open-source technology providers for their learning over a month, the document translations and basic explanations of zephir are coming to an end. we hope to have a better learning and communication with you, as long as the content in this section describes how zephir is handled by the Ted exception mechanism in the function library provided by PHP, let's start the journey of this section. ** Note: The author's level is limited, said the incorrect place I hope you a lot of correction, exchange technology together ** attached: Meow a microphone blog: [w-blog.cn] (w-blog.cn) official zephir website address :[ http://zephir-lang.com/ ] ( http://zephir-lang.com/ "Zephir official website") github address :[ https://github.com/phalcon/zephir ] ( https://github.com/phalcon/zephir ) ## Php functions # PHP has a rich library that you can use in your extensions. To call a PHP function, you only need to use it normally in your Zephir code: namespace MyLibrary; class Encoder {public function encode (var text) {if strlen (text )! = 0 {return base64_encode (text) ;}return false ;}you can call the php function created by the User: namespace MyLibrary; class Encoder {public function encode (var text) {if strlen (text )! = 0 {if function_exists ("my_custom_encoder") {return my_custom_encoder (text) ;}else {return base64_encode (text) ;}} return false ;}} note that all PHP functions only receive and return dynamic variables. If you use static type variables as parameters, a temporary dynamic variable will be automatically used as a bridge to call the function: namespace MyLibrary; class Encoder {public function encode (string text) {if strlen (text )! = 0 {return base64_encode (text) ;}return false ;}similarly, the dynamic value returned by the function cannot be directly allocated to the static variable namespace MyLibrary; class Encoder {public function encode (string text) {string encoded = ""; if strlen (text )! = 0 {let encoded = (string) base64_encode (text); return '('. encoded. ')';} return false;} Zephir dynamically provides a method to call a function, such as namespace MyLibrary; class Encoder {public function encode (var callback, string text) {return {callback} (text) ;}## exception handling ## Zephir achieves exceptions at a very low level and provides PHP with similar behavior and functions. When an exception is thrown, you can use the capture block to capture the exception and allow developers to handle it as appropriate. Try {// Exception can throw new \ exception ("This is an Exception");} catch \ Exception, e {// handle the exception echo e-> getMessage ();} Zephir provides a "try" that has never been responded to, simply ignoring any exception in that block: try {throw new \ Exception ("This is an exception");} a "catch" block can be used to catch different types of exceptions: try {// Exception can throw new \ exception ("This is an Exception");} catch RuntimeException | Exception, e {// handle abnormal echo e-> getMessage ();} Zephir allows you to treat text or static typed variables as abnormal messages: throw "Test "; // Throw new \ Exception (" Test "); throw 't'; // throw new \ Exception (string) 'T'); throw 123; // throw new \ Exception (string) 123); throw 123.123; // throw new \ Exception (string) 123.123); Zephir provides the same facility as PHP exceptions, let you know that an exception has occurred. Exception: getFile () and Exception: getLine () returned location Zephir code has been thrown Exception: the static method 'somemethod' doesn' t exist on model 'robots' File = phalcon/mvc/model. zep Line = 4042 #0/home/scott/test. php (64): Phalcon \ Mvc \ Model ::__ callStatic ('somemethod', Array) #1/home/scott/test. php (64): Robots: someMethod () #2 {main }## summary # this section mainly describes, zephir can directly use PHP function libraries and directly call PHP user-defined functions. In fact, this is the most convenient thing for PHP development, we also give a rough explanation of exception handling, so today's zephir translation and The explanation is here. thank you for your support! Note: I hope you can point out something wrong with my limited ability and have more exchanges! ** Zephir technical exchange: 246348908 welcome to join us! * *** Thanks to zephir developers :**! [] ( http://i.imgur.com/puoG4mx.png )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.