Skip to content
Alex Kiesel edited this page Jun 7, 2012 · 4 revisions

The type system

Every variable in PHP has a type (regardless of what you've heard before: PHP is loosely typed, not untyped). The following types exist:

  • string - Strings
  • integer - Integer numbers, platform-specific
  • double - Floating point numbers
  • boolean - TRUE or FALSE
  • array - Arrays or Hashmaps (or a mix of both)
  • null - The special NULL type
  • resource - Handles to internal data structures

On top of these primitive types, one can define classes and interfaces.

See also http://de3.php.net/types for a detailed overview.

Everything is a Type

The XP framework represents all types by lang.Type instances:

  lang.Object
  `- lang.Type
     `- lang.Primitive
     `- lang.XPClass

Class types

Classes and interfaces are represented by the lang.XPClass class. Instances of XPClass can be retrieved by using its static forName() method and specifying the class name or by calling the instance method getClass() on any object:

  XPClass::forName('lang.Object');  // Represents the lang.Object class
  create(new Object())->getClass(); // (same)

Hierarchy

Every class has the root class lang.Object.

Unfortunately, there is an exception: The lang.Throwable class extends the built-in Exception class due to PHP's ridiculous requirement that anything that can be thrown needs to be a subclass of that!

Primitives

The primitive types are represented by the lang.Primitive class:

<?php
  Primitive::$STRING;    // Represents strings
  Primitive::$INTEGER;   // Represents integer numbers
  Primitive::$DOUBLE;    // Represents floating point numbers
  Primitive::$BOOLEAN;   // Represents boolean values
  Primitive::$ARRAY;     // Represents arrays
?>

The "resource" type has no equivalent.

Wrapper types

All primitive types have wrapper types representing them. These wrapper types can be found in the lang.types package.

The Primitive class provides two static methods to convert between the primitives and their wrapper equivalents.

Primitives -> Wrappers:

<?php
  Primitive::boxed('hello'); // lang.types.String("hello")
  Primitive::boxed(1);       // lang.types.Integer(1)
?>

Wrappers -> Primitives:

<?php
  Primitive::unboxed(new String('hello'));  // string(5) "hello"
  Primitive::unboxed(new Integer(1));       // int(1)
?>