PHP code example of schlaus / schange

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

    

schlaus / schange example snippets


$result = schange::castTo("int", "42");
// OR
$result = schange::castToInt("42");

$result = schange::castTo("banana", 1);
// OR
$result = schange::castToBanana(1);

string("true") => int(1)
string("true") => array("t", "r", "u", "e")
float(4.5) => int(5)
float(4.4) => int(4)
array("tes", "ting", "!", 123) => string("testing!123")
NULL => empty variable of target type

function($var, $currentType) {
	switch($currentType) {
		case "boolean":
			return null;
			break;
		case "integer":
		case "double":
			$arr = str_split(\schlaus\schange\schange::castToStr($var));
			foreach ($arr as &$val) {
				if ($val !== ".") $val = intval($val);
			}
			return $arr;
			break;
		case "string":
			return str_split($var);
			break;
		case "array":
			return $var;
			break;
		case "object":
			return json_decode(json_encode($var), true);
			break;
		case "NULL":
			return array();
			break;
	}

	return null;
}

schange::loadCaster("targetType", function($var, $currentType) {
	/*
	.
	.	Conversion logic goes here
	.
	*/
});

// OR

schange::loadCaster(array(
	"type1" => function($var, $currentType) { ... },
	"type2" => function($var, $currentType) { ... },
));