Skip to main content

Temperature

Temperature differs from other quantities because its scales are offset-based: Celsius and Fahrenheit do not share a zero point with Kelvin, so a single multiplicative conversion factor is not sufficient. Temperature handles this by overriding the conversion logic rather than relying on conversion factors alone.

Native unit

Kelvin (K) is the native unit. Values supplied in other scales are converted to Kelvin on construction, and toUnit() converts from Kelvin back to the requested scale.

Supported scales

ScaleSymbolAliases
KelvinKkelvin, Kelvin
Celsius°CC, celsius, Celsius
Fahrenheit°FF, fahrenheit, Fahrenheit
Rankine°RR, rankine, Rankine

Examples

use Renfordt\UnitLib\Temperature;

// Freezing point of water
$freezing = new Temperature(0, '°C');
$freezing->toUnit('°F'); // 32
$freezing->toUnit('K'); // 273.15

// Boiling point of water
$boiling = new Temperature(100, '°C');
$boiling->toUnit('°F'); // 212
$boiling->toUnit('K'); // 373.15

// Absolute zero
$absolute = new Temperature(0, 'K');
$absolute->toUnit('°C'); // -273.15
$absolute->toUnit('°F'); // -459.67

// Aliases
$temp = new Temperature(72, 'F');
$temp->toUnit('celsius'); // ≈ 22.22

Implementation note

Temperature overrides two methods from the base class:

  • convertToNativeUnit() converts an input value in any scale to Kelvin.
  • toUnit() converts the stored Kelvin value to the requested scale, falling back to the base implementation for units it does not handle.

This is the pattern to follow for any other quantity whose conversions involve an offset rather than a simple ratio. See PhysicalQuantity for where these hooks are called.