Skip to content

Instantly share code, notes, and snippets.

要約

この RFC は4種類のスカラー型、int、float、string と bool の新しい型宣言の追加を提案します。これらの型宣言は PHP 関数が使う既存のメカニズムと同じようにふるまいます。

この RFC はファイル単位でオプションの新しいディレクティブである declare(strict_types=1); をさらに追加することを提案します。このディレクティブによって、エクステンションとビルトインの PHP 関数を含む、すべての関数呼び出しと戻り値の宣言は "strict" なスカラー型の宣言に対して型チェックがなされます。さらに、このディレクティブを伴うエクステンションとビルトインの PHP 関数の呼び出しによって、パラメーター解析が失敗した段階において、E_RECOVERABLE_ERROR が生成され、これらの関数とユーザーランドの型宣言との協調がもたらされます。

これら2つの機能によって、PHP のプログラムは正しく動き、読み書きの前提知識を減らすことができます (self-documenting)。

詳細

@masakielastic
masakielastic / index.html
Last active August 29, 2015 14:17
「reveal.js で pdf のスライドをつくる」のスライド (https://speakerdeck.com/masakielastic/reveal-dot-js-de-pdf-falsesuraidowotukuru) のソースコードです。
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>reveal.js で pdf のスライドをつくる</title>
<meta name="description" content="reveal.js で pdf のスライドをつくる手順を示したスライドです。">
<meta name="author" content="Masaki Kagaya">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/i18next/1.6.3/i18next-1.6.3.min.js"></script>
<script>
var resources = {
en: {
translation: {
"content": {
"access": "Access"
}
@masakielastic
masakielastic / benchmark.php
Last active August 29, 2015 14:23
PRNG Benchmarks (derived from the following code: https://gist.github.com/sarciszewski/f7bd4c0358a44321787b)
<?php
function timer(callable $block) {
$start = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
$block();
}
$end = microtime(true);
@masakielastic
masakielastic / api_stock.php
Last active August 29, 2015 14:25
在庫管理の API を PHP で練習。ストレージに JSON のテキストファイル。
<?php
require 'functions.php';
header('Content-Type: application/json');
$id = empty($_POST['id']) ? null : (int) $_POST['id'];
$amount = empty($_POST['amount']) ? null : (int) $_POST['amount'];
if (!is_int($id) || 0 > $id) {
echo json_encode([
@masakielastic
masakielastic / index.php
Last active August 29, 2015 14:25
1つのファイルで設定画面の練習
<?php
$user = json_decode(file_get_contents('user.json'), true);
if (isset($_POST['email'])) {
$email = $_POST['email'];
$msg = '更新しました。';
$user = ['email' => $email];
file_put_contents('user.json', json_encode($user));
@masakielastic
masakielastic / index.php
Last active August 29, 2015 14:25
1つのファイルでログイン・ログアウトの練習
<?php
session_start();
$login = isset($_POST['username'])
&& $_POST['username'] === 'myuser'
&& isset($_POST['password'])
&& $_POST['password'] === 'mypass';
$logout = isset($_POST['logout'])
@masakielastic
masakielastic / index.php
Created July 27, 2015 11:17
textarea の練習。投稿内容をテキストファイルとして保存。
<?php
if (isset($_POST['message'])) {
file_put_contents('db.txt', $_POST['message']);
$text = $_POST['message'];
} else {
$text = file_get_contents('db.txt');
}
$text = htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
@masakielastic
masakielastic / index.php
Created August 1, 2015 14:08
Facebook PHP SDK v4.0 でユーザーのメールアドレスを取得する
<?php
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.4',
]);
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
$response = $fb->get('/me?locale=en_US&fields=name,email');
@masakielastic
masakielastic / benchmark.php
Last active August 29, 2015 14:27
mt_rand vs chr vs string concatenation
<?php
function timer(callable $block) {
$start = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
$block();
}
$end = microtime(true);
return $end - $start;
}