zephir-(12)php函數和異常處理

來源:互聯網
上載者:User
#zephir-php函數和異常處理#![](http://i.imgur.com/OuZmZ0R.png)##前言##***先在這裡感謝各位zephir開源技術提供者***經過了一個多月的學習,zephir的文檔譯文和基礎講解也將近尾聲了,後面的內容最為重要也希望和大家一同更好的學習交流,本節的內容只要是講解zephir是怎麼時候PHP內建的函數庫已經特德異常機制處理的,那麼讓我們開始本節的旅程把.**注:筆者水平有限,說的不正確的地方希望大家多多指正,一同交流技術**附上:喵了個咪的部落格:[w-blog.cn](w-blog.cn)zephir官網地址:[http://zephir-lang.com/](http://zephir-lang.com/ "zephir官網")github地址:[https://github.com/phalcon/zephir](https://github.com/phalcon/zephir)##php函數##PHP有一個豐富的函數庫,您可以使用在你的擴充。 調用PHP函數只需正常使用它在你Zephir代碼:namespace MyLibrary;class Encoder{    public function encode(var text)    {        if strlen(text) != 0 {            return base64_encode(text);        }        return false;    }}你可以調用使用者建立的php函數: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;    }}注意所有PHP函數只接收和返回動態變數。 如果你通過靜態類型變數作為參數,一個臨時的動態變數將自動被用作橋為了調用函數:namespace MyLibrary;class Encoder{    public function encode(string text)    {        if strlen(text) != 0 {            return base64_encode(text);        }        return false;    }}類似地,函數返回動態值不能直接分配給靜態變數: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動態提供了一種方法來調用函數,如:namespace MyLibrary;class Encoder{    public function encode(var callback, string text)    {        return {callback}(text);    }}##異常處理##Zephir實現異常在很低的水平,為PHP提供類似的行為和功能。拋出異常時,可以使用“捕捉”塊捕獲異常並允許 開發人員提供適當的處理。try {    //異常都可以在這裡拋出    throw new \Exception("This is an exception");} catch \Exception, e {    //處理異常    echo e->getMessage();}Zephir提供了一直沒有反應的“try”,簡單地忽略任何異常在那塊:try {    throw new \Exception("This is an exception");}一個“catch ”塊可以用來捕獲不同類型的異常:try {    //異常都可以在這裡拋出    throw new \Exception("This is an exception");} catch RuntimeException|Exception, e {    //處理異常    echo e->getMessage();}Zephir允許你把文字或靜態類型化變數當作異常的訊息: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作為PHP的異常提供相同的設施,讓你知道發生了異常。 例外::getFile()和異常:getLine()返回位置Zephir代碼已經被拋出的異常: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}##總結##本節主要講解了,zephir可以直接使用PHP的函數庫並且可以直接調用PHP使用者定義的函數,其實這個是對PHP開發這最方便的一件事情,還有對異常處理進行了一些粗略的講解,那麼今天的zephir譯文和講解就到這裡了,多謝大家的支援!注:筆者能力有限有說的不對的地方希望大家能夠指出,也希望多多交流!**zephir技術交流:246348908 歡迎大家的加入!****感謝zephir開發人員:**![](http://i.imgur.com/puoG4mx.png)
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.