PHP 個人リファレンス

目次

チートシート

例外処理

<?php

declare(strict_types=0);

// 例外処理

function throwException(string $message): void
{
    echo "Exception\n";
    throw new Exception($message);
}

function throwLogic(string $message): void
{
    echo "LogicException\n";
    throw new LogicException($message);
}

function throwBadFunctionCall(string $message): void
{
    echo "BadFunctionCallException\n";
    throw new BadFunctionCallException($message);
}

function throwBadMethodCall(string $message): void
{
    echo "BadMethodCallException\n";
    throw new BadMethodCallException($message);
}

try {
    throwLogic('error');
} catch (Exception $e) {
    echo $e->getMessage();
}

クラスが関数の引数に使われる調査

namespace Test;

class Hoge
{
    public $val;

    public function __construct(string $val)
    {
        $this->val = $val;
    }

    public function hogehoge(): void
    {
        $this->val = $this->val.'hogeClass';
    }
}

function useHoge(Hoge $hoge): string
{
    $hoge->hogehoge();

    return $hoge->val;
}

echo useHoge(new Hoge('hoge--'));tMessage();
}