SPL Type Handling

Inhaltsverzeichnis

The SplType class

Einführung

Parent class for all SPL types.

Klassenbeschreibung

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

Vordefinierte Konstanten

SplType::__default

The SplInt class

Einführung

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

Klassenbeschreibung

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

Vordefinierte Konstanten

SplInt::__default

Beispiele

Beispiel #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;
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Value not an integer
94

The SplFloat class

Einführung

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

Klassenbeschreibung

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

Vordefinierte Konstanten

SplFloat::__default

Beispiele

Beispiel #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;
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Value not a float
3.154
3

Die Klasse SplEnum

Einführung

SplEnum bietet die Möglichkeit Aufzählungen (Enums) und Enum-Objekte nativ in PHP zu erstellen.

Klassenbeschreibung

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

Vordefinierte Konstanten

SplEnum::__default

Beispiele

Beispiel #3 SplEnum Verwendungsbeispiel

<?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;
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

6
Value not a const in enum Month

The SplBool class

Einführung

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

Klassenbeschreibung

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

Vordefinierte Konstanten

SplBool::__default

SplBool::false

SplBool::true

Beispiele

Beispiel #1 SplBool usage example

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

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

TRUE

The SplString class

Einführung

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

Klassenbeschreibung

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

Vordefinierte Konstanten

SplString::__default

Beispiele

Beispiel #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"
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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