This page is a translated version of the page Assert and the translation is 62% complete.

Assert is a PHP library that provides an alternative to PHP's assert() that allows for a simple and reliable way to check preconditions and postconditions in PHP code. MediaWiki RFCとして提案されましたが、完全に一般的であり、任意の PHP プログラムまたはライブラリで使用できます。


Assertクラスは、さまざまな種類のアサーションをチェックするためのいくつかの静的メソッドを提供します。 The most common kind is to check the type of a parameter, typically in a constructor or a setter method:

use Wikimedia\Assert\Assert;

function setFoo( $foo ) {
    Assert::parameterType( 'integer', $foo, 'foo' );
    Assert::parameter( $foo > 0, 'foo', 'must be greater than 0' );

function __construct( $bar, array $bazz ) {
    Assert::parameterType( 'Me\MyApp\SomeClass', $bar );
    Assert::parameterElementType( 'int', $bazz );

Checking parameters, or other assertions such as pre- or postconditions, is not recommended for performance critical regions of the code, since evaluating expressions and calling the assertion functions costs time.