99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

Typechecker簡介

2018-10-27 11:20 更新

Hack typechecker是使Hack成為一種獨特語言的主要工具。在運行時間之前,typechecker會分析與程序相關(guān)的所有與各種打字錯誤相關(guān)的代碼,從而防止可能只在運行時暴露的惡作劇。

沒有typechecker,這個簡單的例子將在運行時失敗。

<?php

namespace Hack\UserDocumentation\TypeChecker\Intro\Examples\RuntimeFail;

class A {
  public function foo() { return 2; }
}

function failing($x) {
  $a = new A();
  if ($x === 4) {
    $a = null;
  }
  // $a being null would only be caught at runtime
  // Fatal error: Uncaught exception 'BadMethodCallException' with message
  // 'Call to a member function foo() on a non-object (NULL)'
  $a->foo();
}

failing(4);

Output

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function foo() on a non-object (null)' in /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/runtime-fail.php:17
Stack trace:
#0 /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/runtime-fail.php(20): Hack\UserDocumentation\TypeChecker\Intro\Examples\RuntimeFail\failing()
#1 {main}

但是,在運行代碼之前使用typechecker,您可以在部署之前捕獲錯誤,從而將用戶從可能發(fā)生的惡意致命中保存。

<?hh

namespace Hack\UserDocumentation\TypeChecker\Intro\Examples\TypecheckerCatch;

class A {
  public function foo() { return 2; }
}

function failing($x) {
  $a = new A();
  if ($x === 4) {
    $a = null;
  }
  // $a being null would only be caught BEFORE runtime
  //   typechecker-catch.php:21:7,9: You are trying to access the member foo
  //                                 but this object can be null. (Typing[4064])
  //   typechecker-catch.php:12:10,13: This is what makes me believe it can be
  //                                   null
  //
  $a->foo();
}

failing(4);

Output

Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function foo() on a non-object (null)' in /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/typechecker-catch.php.type-errors:20
Stack trace:
#0 /data/users/joelm/user-documentation/guides/hack/25-typechecker/01-introduction-examples/typechecker-catch.php.type-errors(23): Hack\UserDocumentation\TypeChecker\Intro\Examples\TypecheckerCatch\failing()
#1 {main}

typechecker靜態(tài)分析您的程序,捕捉代碼所有路徑中的問題。這不是匯編。它是對程序中可能出現(xiàn)的各種狀態(tài)的超快速反饋,因此您可以在代碼運行之前處理它們。Typechecker可以實時監(jiān)控代碼的更改并相應(yīng)地更新其分析。

結(jié)合類型注釋的功能,Typechecker是一個強大的錯誤捕獲工具。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號