Skip to content

PhpValue#

assembly: Peachpie.Runtime.dll

PHP is a loosely typed language. It specifies the value of a variable not to be restricted to a single type. This is referred to as type ambiguity, type dynamicity, or is also known as mixed.

PeachPie defines struct PhpValue as the abstraction of PHP's mixed. This .NET type is used in case a type of value cannot be resolved in compile time. It applies to global variables in general, function parameters without a type hint, function return types without a type hint, class properties and local variables.

Conversions#

To PhpValue#

Any .NET type can be converted to PhpValue. The following table lists the types and the corresponding conversion function.

.NET type Conversion routine Sample
bool implicit cast (PhpValue)true
int implicit cast (PhpValue)(-1)
long implicit cast (PhpValue)9876543210L
double implicit cast (PhpValue)123.456
string implicit cast (PhpValue)"Hello"
byte[] implicit cast (PhpValue)Encoding.UTF8.GetBytes("Hello")
PhpArray implicit cast (PhpValue)new PhpArray()
PhpArray represents PHP's array object.
delegate implicit cast (PhpValue)new Func<int>(() => 123)
In PHP, the delegate is treated as an instance of a callable class.
object FromClass() PhpValue.FromClass(new System.Object())

From PhpValue#

A value can be converted to a standard .NET type using the following conversion routines. When applicable, the conversion respects the PHP semantic of the casting values.

.NET type Conversion routine Sample Remarks
bool implicit cast (bool)value -
ushort explicit cast (ushort)value OverflowException, PHP notice
int explicit cast (int)value OverflowException, PHP notice
uint explicit cast (uint)value OverflowException, PHP notice
long explicit cast (long)value PHP notice
double explicit cast (double)value PHP notice
string explicit cast value.ToString() byte[] is encoded using UTF-8
PhpNumber explicit cast (PhpNumber)value InvalidCastException
PhpArray explicit cast (PhpArray)value conversion to array according to PHP semantic
PhpAlias EnsureAlias() value.EnsureAlias() -
Object AsObject() value.AsObject() gets underlaying object, dereferenced, can be null
Object ToClr() #c# value.ToClr() boxes contained value into System.Object, can be null.

Operators#

The type defines arithmetic and comparison operators according to PHP semantic.

Operator Sample Remarks
== val1 == val2 non-string equality
!= val1 != val2 non-strict inequality
< val1 < val2 comparison
~ ~val1 bit negation
& val1 & val2 bit and
| val1 | val2 bit or
^ val1 ^ val2 bit xor
/ val1 / val2 division, operands are converted to number
* val1 * val2 multiplication, operands are converted to number
[] val[1] accessing value item as array according to PHP semantic
StrictEquals val1.StrictEquals(val2) strict equality, PHP' ===
ToArray val.ToArray() conversion to array according to PHP semantic
AsArray val.AsArray() gets underlaying PhpArray or null
AsCallable val.AsCallable() gets IPhpCallable from PHP' callable which can be invoked, can be null