PHPackages                             schlaus/schange - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. schlaus/schange

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

schlaus/schange
===============

A type juggler

1.0.1(11y ago)021MITPHPPHP &gt;=5.3.0

Since Aug 22Pushed 11y ago1 watchersCompare

[ Source](https://github.com/schlaus/schange)[ Packagist](https://packagist.org/packages/schlaus/schange)[ Docs](https://github.com/schlaus/schange)[ RSS](/packages/schlaus-schange/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

[![Build Status](https://camo.githubusercontent.com/228d5454d9909ef276acfa5d0323daafb4e8b0139b4b8c73de06a72a9c1eebee/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f7363686c6175732f736368616e67652e706e67)](http://travis-ci.org/schlaus/schange)[![Latest Stable Version](https://camo.githubusercontent.com/bb25103c345e8f938406697112d086c0973414130bd428f945fc4f52475b5903/68747470733a2f2f706f7365722e707567782e6f72672f7363686c6175732f736368616e67652f76657273696f6e2e737667)](https://packagist.org/packages/schlaus/schange)[![Total Downloads](https://camo.githubusercontent.com/7a4f3864377957c54e74b69b286dac5524e05e9c16e15f4063e6889121783589/68747470733a2f2f706f7365722e707567782e6f72672f7363686c6175732f736368616e67652f646f776e6c6f6164732e737667)](https://packagist.org/packages/schlaus/schange)[![License](https://camo.githubusercontent.com/5c7f44b75df13f5341882d9eef4adac310ca3e13282b88e5d9011aa914e90699/68747470733a2f2f706f7365722e707567782e6f72672f7363686c6175732f736368616e67652f6c6963656e73652e737667)](https://packagist.org/packages/schlaus/schange)

Schange
=======

[](#schange)

An extendable type juggler.

Installation
------------

[](#installation)

Either via [Composer](https://packagist.org/packages/schlaus/schange) by requiring "schlaus/schange": "dev-master"

Or just download and include src/schange.php

Usage
-----

[](#usage)

What Schange does is to try to cast a variable to a given type by doing some conversions. The conversion logic is based on my usecase, and could be somewhat off or completely ludicrous in different situations. Parental discretion is adviced.

Syntax is:

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

Once a custom casting function has been loaded, it can be used in the exact same fashion:

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

The code and tests are pretty straightforward, and should give a pretty good idea of what kind of conversions are done. Supported types are *boolean*, *integer*, *string*, *array*, *float*, and *object*. Here's a few examples:

```
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
```

Furthermore, associative arrays can be converted to objects (instances of stdClass) with each key =&gt; value converted into property =&gt; value. When converting from objects to arrays, only public properties are preserved. When converting an object to string, the conversion is first attempted via the magic \_\_toString() method, and if one doesn't exist, the object is first converted to an array, and then to string. Null is only ever returned if given variable can't be cast to requested type, for example when trying to convert a boolean value to an array. If the original variable was null, it's converted to an empty instance of the requested type.

For examining whether a variable can be converted to a type, use schange::canCastTo($type, $var); For a list (as an array) of possible target types, use schange::castable($var);

### Using custom casters

[](#using-custom-casters)

Casting functions receive two parameters: the variable to be cast, and the current type of the variable. Thus each function can only handle casting to one target type. For an example of how a casting function should look like, let's look at the castToArr function:

```
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;
}
```

Casting functions should return null only when the requested conversion can't be done, and in case the input was null to begin with, an empty instance of the target type. There are no separate functions for evaluating whether a variable can be cast to another type, but instead the testing is done using the actual casting function, and a result returned based on whether the result was null or not.

Custom casters can be loaded either one function at a time, or as an array of casters:

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

// OR

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

Once loaded, custom functions can be used just in the same way as the default ones. Custom casters take precedence over the default ones, so for example if you register a function for casting to string, that function will be used instead of the default one.

One thing to note when writing custom functions is that if you want to support PHP 5.3 you can't use *self::...* in your function, since in PHP 5.3 the closure is not run in the correct scope. Instead, use the full namespaced class name.

DISCLAIMER:
-----------

[](#disclaimer)

I made this class for my own specific purposes, and don't expect it to be immensely useful to other people. However, I'm happy to help if you do find some use for it and run into problems.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

4287d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c3a43b3880ecbadd178a300fa7f0ff173dc56622c506c9f85822864f658df5d6?d=identicon)[schlaus](/maintainers/schlaus)

---

Tags

conversioncasttypejuggling

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/schlaus-schange/health.svg)

```
[![Health](https://phpackages.com/badges/schlaus-schange/health.svg)](https://phpackages.com/packages/schlaus-schange)
```

###  Alternatives

[webpatser/laravel-uuid

Laravel integration for webpatser/uuid - High-performance drop-in UUID replacements (15% faster than Ramsey). Provides Str macros, HasUuids trait, facades, and casts. RFC 4122/9562 compliant.

1.8k17.3M129](/packages/webpatser-laravel-uuid)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[florianv/swap

Exchange rates library for PHP

1.3k6.4M16](/packages/florianv-swap)[spatie/color

A little library to handle color conversions

38018.9M28](/packages/spatie-color)[php-units-of-measure/php-units-of-measure

A PHP library for converting between standard units of measure.

3123.4M20](/packages/php-units-of-measure-php-units-of-measure)[florianv/laravel-swap

Currency exchange rates library for Laravel and Lumen

3342.0M2](/packages/florianv-laravel-swap)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
