PHP code example of rikudou / scalar-objects

1. Go to this page and download the library: Download rikudou/scalar-objects 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/ */

    

rikudou / scalar-objects example snippets




use Rikudou\ScalarObjects\TypeHint\IntegerTypehint;
use Rikudou\ScalarObjects\TypeHint\StringTypehint;

e two variables are not actually objects, the typehints
// are just for IDE completion

var_dump($num1->isInt()); // bool(true)
var_dump($num2->isString()); // bool(true)
var_dump($num2->isNumeric()); // bool(true)
var_dump($num2->isNumber()); // bool(false) - isNumber() returns true only for int and float

var_dump($num1->toString()->length()); // int(1)

/** @var StringTypehint $string */
$string = "This is a test string";

var_dump($string->length()); // int(21);
var_dump($string->capitalize()); // string(21) "This Is A Test String"
var_dump($string->caseInsensitiveCompare("this Is a TESt STRInG")); // int(0)
var_dump($string->toUpper()); // string(21) "THIS IS A TEST STRING"
var_dump($string->toLower()); //string(21) "this is a test string"




use Rikudou\ScalarObjects\TypeHint\IntegerTypehint;
use Rikudou\ScalarObjects\TypeHint\StringTypehint;

_dump($string); // string(8) "testtest"

/** @var IntegerTypehint|int $int1 */
$int1 = 5;
/** @var IntegerTypehint|int $int2 */
$int2 = -10;
/** @var IntegerTypehint|int $int3 */
$int3 = "-15";

var_dump($int1 + $int2->abs()); // int(15)
var_dump($int1 + $int2); // int(-5)
var_dump($int1 + $int2 + $int3->toInt()->abs()); // int(10)