<?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}
<?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}
更多建議: