Oihana PHP System

Version implements Equatable

Represents a software version using four components: major, minor, build, and revision.

These components are internally encoded into a single 32-bit integer for compact storage and efficient comparison.

Usage Example:

use oihana\reflections\Version ;

$version1 = new Version( 2 , 1 , 1 , 110 ) ;
$version2 = new Version( 3 , 1 , 1 , 110 ) ;
$version3 = new Version( 1 , 2 , 3 , 4 ) ;

$version1->major = 3 ;

echo( 'version  : ' . $version1 ) ;
echo( 'major    : ' . $version1->major ) ;
echo( 'minor    : ' . $version1->minor ) ;
echo( 'build    : ' . $version1->build ) ;
echo( 'revision : ' . $version1->revision ) ;

echo( 'version 1 : ' . $version1->valueOf() ) ;
echo( 'version 2 : ' . $version2->valueOf() ) ;
echo( 'version 3 : ' . $version3->valueOf() ) ;

echo( "equals( 'toto' )    : " . ($version1->equals('toto')    ? 'true' : 'false' )) ;
echo( "equals( $version2 ) : " . ($version1->equals($version2) ? 'true' : 'false' )) ;
echo( "equals( $version3 ) : " . ($version1->equals($version3) ? 'true' : 'false' )) ;

Key Features:

  • Supports dynamic property access ($version->major, $version->minor, etc.) through magic methods.
  • Efficiently encodes and decodes version components using bitwise operations.
  • Customizable string representation via the separator and fields properties.
  • Implements equality checking through the equals() method.
  • Provides a fromString() static method for instantiating versions from formatted strings.

Table of Contents

Interfaces

Equatable
This interface is implemented by all equatable objects.

Properties

$build  : int
Gets or sets the build component (bits 16–23).
$fields  : int
Specifies how many version components should be included in the string representation.
$major  : int
Gets or sets the major version component (stored in the highest 4 bits).
$minor  : int
Gets or sets the minor version component (bits 24–27).
$revision  : int
Gets or sets the revision component (lowest 16 bits).
$separator  : string
The string used to separate version components when casting to string.
$_value  : int
The internal integer that encodes the four version components.

Methods

__construct()  : mixed
Creates a new Version instance from individual components.
__toString()  : string
Returns the string representation of the version, respecting `fields` and `separator`.
equals()  : bool
Checks whether the current instance is equal to another version.
fromString()  : string|null
Instantiates a Version from a formatted version string (e.g., "1.2.3.4").
valueOf()  : int
Returns the internal 32-bit integer value representing the version.
RRR()  : int
Bitwise logical right shift (unsigned), emulates `>>>` operator.

Properties

$build virtual

Gets or sets the build component (bits 16–23).

public int $build
Hooks
public int get public set
Parameters
$value : int

$fields

Specifies how many version components should be included in the string representation.

public int $fields = 0

Values can range from 1 to 4. Defaults to 0, which means automatic trimming of trailing zeroes.

$major virtual

Gets or sets the major version component (stored in the highest 4 bits).

public int $major
Hooks
public int get public set
Parameters
$value : int

$minor virtual

Gets or sets the minor version component (bits 24–27).

public int $minor
Hooks
public int get public set
Parameters
$value : int

$revision virtual

Gets or sets the revision component (lowest 16 bits).

public int $revision
Hooks
public int get public set
Parameters
$value : int

$separator

The string used to separate version components when casting to string.

public string $separator = \oihana\enums\Char::DOT

$_value

The internal integer that encodes the four version components.

private int $_value

Methods

__construct()

Creates a new Version instance from individual components.

public __construct([int $major = 0 ][, int $minor = 0 ][, int $build = 0 ][, int $revision = 0 ]) : mixed
Parameters
$major : int = 0

The major version number (4 bits).

$minor : int = 0

The minor version number (4 bits).

$build : int = 0

The build number (8 bits).

$revision : int = 0

The revision number (16 bits).

__toString()

Returns the string representation of the version, respecting `fields` and `separator`.

public __toString() : string
Return values
string

The stringified version (e.g., "1.2.3").

equals()

Checks whether the current instance is equal to another version.

public equals(mixed $value) : bool
Parameters
$value : mixed

The value to compare to (typically another Version instance).

Tags
example
$v1 = new Version( 1,0,0,0 );
$v2 = new Version( 1,0,0,0 );
echo( json_encode( v1->equals( v2 ) ) ) ; //true

A cast to Number/int force the valueOf, not ideal but sufficient, and the same for any other operators. But as we keep Equatable for now, then we have no reason to not use it.

Return values
bool

True if both instances represent the same version; false otherwise.

fromString()

Instantiates a Version from a formatted version string (e.g., "1.2.3.4").

public static fromString(string $value[, string $separator = Char::DOT ]) : string|null
Parameters
$value : string

The string to parse.

$separator : string = Char::DOT

The separator to use (defaults to .).

Return values
string|null

A stringified version object or null if parsing fails.

valueOf()

Returns the internal 32-bit integer value representing the version.

public valueOf() : int
Return values
int

The packed version number.

RRR()

Bitwise logical right shift (unsigned), emulates `>>>` operator.

private RRR(int $a, int $b) : int
Parameters
$a : int

The integer to shift.

$b : int

The number of bits to shift.

Return values
int

The result of the shift.


        
On this page

Search results