PHP code example of joebengalen / assert

1. Go to this page and download the library: Download joebengalen/assert library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joebengalen / assert example snippets


use JoeBengalen\Assert\Assert;

/**
 * @param int  $arg1
 * @param bool $arg2
 */
function foo($arg1, $arg2)
{
    // Make sure the arguments are indeed what they should be
    Assert::isInteger($arg1);
    Assert::isBoolean($arg2);

    // Actual code ...
}

// This will be fine ...
foo(12, true);

// ... but this will throw an InvalidArgumentException
foo(2, 4);

Assert::isBoolean(3, 'Custom message: got %s, but expected boolean');
// Throws: "Custom message: got integer, but expected boolean"

Assert::isInstanceOf(new Foo, 'Bar', 'Custom message: got %s, but expected instance of %s');
// Throws: "Custom message: got Foo, but expected instance of Bar"

Assert::isInstanceOf(new Foo, 'Bar', 'Custom message: expected instance of %2$s, but got %s');
// Throws: "Custom message: expected instance of Bar, but got Foo"