PHPackages                             its-mieger/obj - 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. its-mieger/obj

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

its-mieger/obj
==============

Library for implementing native data type behaviour (comparision, conversion, ...) for objects

1.4.0(5y ago)01451proprietaryPHPPHP &gt;=7.0CI failing

Since Feb 15Pushed 5y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (10)Used By (1)

ItsMieger Obj - Native data type behavior for PHP objects
=========================================================

[](#itsmieger-obj---native-data-type-behavior-for-php-objects)

This library helps to implement behavior like native data types (casts, comparision, conversion, operator overloading ...) for custom PHP objects.

Since PHP does not support operator overloading, custom helper functions are implemented to apply operations using the implemented behavior.

Following interfaces are available:

InterfacePurposeComparableObject can be compared with others (overrides comparision operators)CastsAsBooleanObject can be represented as booleanCastsAsFloatObject can be represented as floatCastsAsIntegerObject can be represented as integerCastsAsStringObject can be represented as stringOperatorAddObject implements custom add operatorOperatorSubtractObject implements custom subtract operatorOperatorMultiplyObject implements custom multiply operatorOperatorDivideObject implements custom divide operatorNullableObject can represent nullHelper functions
----------------

[](#helper-functions)

Since PHP does not support operator overloading special helper functions must be used to make objects behave as implemented by the specified interfaces. However helper functions are not autoloaded but may be imported by calling `Obj::loadHelpers()`. If not loaded, the helpers may be called using the static methods of the `Obj`-class

NativeHelper functionCall without helper function`==``o_eq``Obj::equal(...)``!=``o_ne``Obj::notEqual(...)````o_comp``Obj::compare(...)``=``o_ge``Obj::greaterThanOrEqual(...)``+``o_add``Obj::add(...)``-``o_sub``Obj::subtract(...)``*``o_mul``Obj::multiply(...)``/``o_div``Obj::divide(...)``isFloat()``o_isFloat``Obj::isFloat(...)``isInt()``o_isInt``Obj::isInt(...)``isBool()``o_isBool``Obj::isBool(...)``isString()``o_isString``Obj::isString(...)``(float)``o_castFloat``Obj::castFloat(...)``(int)``o_castInt``Obj::castInt(...)``(bool)``o_castBool``Obj::castBool(...)``(string)``o_castString``Obj::castString(...)``o_comparePipe``Obj::comparePipe(...)`Behavior
--------

[](#behavior)

Using one of the object helper functions, objects behave like native data types would do for the corresponding native operator.

Example:

```
class MyInt implements CastsAsInteger {

 	public function toInt(): int {
 		return 3;
 	}

}
$a = new MyInt();

$result = op_add($a, 2);

```

In this example `$result` will be `5`. Adding an integer to the object will call the `toInt()`-function to cast the object as integer and than execute the operation.

**Important** You should not implement more than one `CastsAs`\*-interface in one class. Of you would do the object might behave unexpected since in some situations it might be converted eg. to `int` and in others to `float`. Rather than implementing both interface, you should only implement eg. `int`. If a float value is required, the returned int value is casted to `float` automatically.

### Overriding operators

[](#overriding-operators)

Sometimes casting objects is not enough. Imagine a custom Money implementation where you want to add two Money objects and want to receive a new Money object instead of a scalar number. Therefore you may implement one of the `Operator`\*-interfaces and define your custom operator.

#### How operators are applied

[](#how-operators-are-applied)

Sometimes it is not obvious which operator function is called. Especially if having two objects implementing the same operator. The behavior described below concerns the `Operator`\*-interfaces as well as the `Comparable`-interface which overrides all comparision operators with one function:

- If both implement the operator interface, the operator function is invoked for the descended object if the objects are descendants and the operation is commutative (the compare operation is also treated as commutative). If objects are not descendants, the operator function is invoked for the left side object.
- If only one implements the Operator interface, the operator function is invoked for the corresponding object.
- If none implements the operator interface the objects are converted to their native representation and the native operator is applied.

Testing
-------

[](#testing)

You may mock helpers by creating a mock object for the `ObjectHelper`-class and pass it to `Obj::mock(...)`. This way all calls to object helpers (via helper functions or static interface) are redirected to the mocked object. Call `Obj::resetMock()` to remove the mocked object und make object logic be used again.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity64

Established project with proven stability

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 ~128 days

Recently: every ~257 days

Total

9

Last Release

1985d ago

PHP version history (2 changes)1.0.0PHP ~7.0

1.4.0PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/d4062a57c91cdead9169f9e84797ada75bc2df8867a632d11521dbbff9e3231a?d=identicon)[its-mieger](/maintainers/its-mieger)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/its-mieger-obj/health.svg)

```
[![Health](https://phpackages.com/badges/its-mieger-obj/health.svg)](https://phpackages.com/packages/its-mieger-obj)
```

###  Alternatives

[ffi/location

PHP library for determining the physical location of binaries

1339.8k7](/packages/ffi-location)[getkirby/geo

Kirby Geo

223.5k](/packages/getkirby-geo)

PHPackages © 2026

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