case 0
Native
try {
$pdo = new PDO($db['dsn'], $db['username'], $db['password'], $options);
} catch (PDOException $e) {
throw new PDOException('数据库连接失败:' . $e->getMessage());
}
Option
/** @var PDO $conn */
Some(!$pdo->errorCode())->expect(new PDOException("数据库链接失败" . $pdo->errorCode()));
Native
$statement = $pdo->query("select count(id) from link");
$cnt = 0;
if (!$statement) {
$cnt = $statement->rowCount();
}
Option
$cnt = Some($pdo->query("select count(id) from link"))->andThen(function ($statement) {
return $statement->rowCount();
})->unwrapOr(0);
print_r($cnt);
Native
$query = $pdo->query("select id,name from link");
$result = [];
if ($query) {
$res = $query->fetchAll();
if ($res) {
foreach ($res as $key => $item) {
$result[] = [
"link_id" => $item["id"],
"link_name" => $item["name"] ?? "匿名链接",
];
}
}
}
Option
$result = Some($pdo->query("select id,name from link"))->andThen(function ($state) {
return $state->fetchAll();
})->filter(function ($item) {
return [
"link_id" => $item["id"],
"link_name" => Some($item["name"])->unwrapOr("匿名链接"),
];
})->unwrapOr([]);
print_r($result);
case 1
// Option
$res = Some($db->get())->expect(new \Exception("msg"));
// native
$res = $db->get();
if (is_null($res)) {
throw new \Exception("msg");
}
case2
// Option
$_POST["hello"] = null;
Some($_POST['hello'])->unwrapOr("hi"); // hi
$_POST["hello"] = "hello";
Some($_POST['hello'])->unwrapOr("hi"); // hello
// ... but we have $_POST["hello"]??"hi" 2333
Case3
// scenario
$obj = new Obj();
$obj->attr = null; // attr is Object;
// Option
// None
$attr = Some($obj->attr)->andThen(function($attr){
return "Some Data";
})->unwrapOr("no success");
//Some
$attr = Some(1)->andThen(function($attr){
return null;
})->unwrapOr("no success");
// native
$attr = "no success";
if ($obj->attr) {
$attr = (function($attr){return ""; })()
}
if (!$attr) {
$attr = "no success";
}