SPL Type Handling

Table of Contents

The SplType class

Introduction

Parent class for all SPL types.

Class synopsis

SplType
abstract class SplType {
/* Constants */
const NULL SplType::__default = null ;
/* Methods */
__construct ([ mixed $initial_value [, bool $strict ]] )
}

Predefined Constants

SplType::__default

The SplInt class

Introduction

The SplInt class is used to enforce strong typing of the integer type.

Class synopsis

SplInt
class SplInt extends SplType {
/* Constants */
const integer SplInt::__default = 0 ;
/* Inherited methods */
SplType::__construct ([ mixed $initial_value [, bool $strict ]] )
}

Predefined Constants

SplInt::__default

Examples

Example #1 SplInt usage example

<?php
$int 
= new SplInt(94);

try {
    
$int 'Try to cast a string value for fun';
} catch (
UnexpectedValueException $uve) {
    echo 
$uve->getMessage() . PHP_EOL;
}

echo 
$int PHP_EOL;
?>

The above example will output:

Value not an integer
94

The SplFloat class

Introduction

The SplFloat class is used to enforce strong typing of the float type.

Class synopsis

SplFloat
class SplFloat extends SplType {
/* Constants */
const float SplFloat::__default = 0 ;
/* Inherited methods */
SplType::__construct ([ mixed $initial_value [, bool $strict ]] )
}

Predefined Constants

SplFloat::__default

Examples

Example #2 SplFloat usage example

<?php
$float 
= new SplFloat(3.154);
$newFloat = new SplFloat(3);

try {
    
$float 'Try to cast a string value for fun';
} catch (
UnexpectedValueException $uve) {
    echo 
$uve->getMessage() . PHP_EOL;
}

echo 
$float PHP_EOL;
echo 
$newFloat PHP_EOL;
?>

The above example will output:

Value not a float
3.154
3

The SplEnum class

Introduction

SplEnum gives the ability to emulate and create enumeration objects natively in PHP.

Class synopsis

SplEnum
class SplEnum extends SplType {
/* Constants */
const NULL SplEnum::__default = null ;
/* Methods */
public array getConstList ([ bool $include_default = false ] )
/* Inherited methods */
SplType::__construct ([ mixed $initial_value [, bool $strict ]] )
}

Predefined Constants

SplEnum::__default

Examples

Example #3 SplEnum usage example

<?php
class Month extends SplEnum {
    const 
__default self::January;
    
    const 
January 1;
    const 
February 2;
    const 
March 3;
    const 
April 4;
    const 
May 5;
    const 
June 6;
    const 
July 7;
    const 
August 8;
    const 
September 9;
    const 
October 10;
    const 
November 11;
    const 
December 12;
}

echo new 
Month(Month::June) . PHP_EOL;

try {
    new 
Month(13);
} catch (
UnexpectedValueException $uve) {
    echo 
$uve->getMessage() . PHP_EOL;
}
?>

The above example will output:

6
Value not a const in enum Month

The SplBool class

Introduction

The SplBool class is used to enforce strong typing of the bool type.

Class synopsis

SplBool
class SplBool extends SplEnum {
/* Constants */
const boolean SplBool::__default = false ;
const boolean SplBool::false = false ;
const boolean SplBool::true = true ;
/* Inherited methods */
public array SplEnum::getConstList ([ bool $include_default = false ] )
}

Predefined Constants

SplBool::__default

SplBool::false

SplBool::true

Examples

Example #1 SplBool usage example

<?php
$true 
= new SplBool(true);
if (
$true) {
    echo 
"TRUE\n";
}

$false = new SplBool;
if (
$false) {
    echo 
"FALSE\n";
}
?>

The above example will output:

TRUE

The SplString class

Introduction

The SplString class is used to enforce strong typing of the string type.

Class synopsis

SplString
class SplString extends SplType {
/* Constants */
const string SplString::__default = '' ;
/* Inherited methods */
SplType::__construct ([ mixed $initial_value [, bool $strict ]] )
}

Predefined Constants

SplString::__default

Examples

Example #2 SplString usage example

<?php
$string 
= new SplString("Testing");

try {
    
$string = array();
} catch (
UnexpectedValueException $uve) {
    echo 
$uve->getMessage() . PHP_EOL;
}

var_dump($string);
echo 
$string// Outputs "Testing"
?>

The above example will output:

Value not a string
object(SplString)#1 (1) {
  ["__default"]=>
  string(7) "Testing"
}
Testing