Book/spl-types-Phpdoc专题
SPL Type Handling
目录
- 简介
- 安装/配置
- SplType — The SplType class
- SplType::__construct — Creates a new value of some type
- SplInt — The SplInt class
- SplFloat — The SplFloat class
- SplEnum — The SplEnum class
- SplEnum::getConstList — Returns all consts (possible values) as an array
- SplBool — The SplBool class
- SplString — The SplString class
简介
Parent class for all SPL types.
类摘要
SplType
abstract class SplType {
/* Constants */
const NULL
SplType::__default = null ;
/* 方法 */
__construct ([ <span
class="methodparam">mixed
$initial_value [, <span
class="type">bool $strict ]] )
}
预定义常量
SplType::__default
SplType::__construct
Creates a new value of some type
说明
SplType::__construct ([ <span
class="methodparam">mixed
$initial_value [, <span
class="type">bool $strict ]] )
Warning
本函数还未编写文档,仅有参数列表。
参数
initial_value
Type and default value depends on the extension class.
strict
Whether to set the object's strictness.
错误/异常
Throws an UnexpectedValueException if incompatible type is given.
简介
The SplInt class is used to enforce strong typing of the integer type.
类摘要
SplInt
class SplInt extends SplType {
/* Constants */
const int
SplInt::__default = 0 ;
/* 继承的方法 */
SplType::__construct ([ <span
class="methodparam">mixed
$initial_value [, <span
class="type">bool $strict ]] )
}
预定义常量
SplInt::__default
范例
示例 #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;
?>
以上例程会输出:
Value not an integer
94
简介
The SplFloat class is used to enforce strong typing of the float type.
类摘要
SplFloat
class SplFloat extends SplType {
/* Constants */
const float
SplFloat::__default = 0 ;
/* 继承的方法 */
SplType::__construct ([ <span
class="methodparam">mixed
$initial_value [, <span
class="type">bool $strict ]] )
}
预定义常量
SplFloat::__default
范例
示例 #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;
?>
以上例程会输出:
Value not a float
3.154
3
简介
SplEnum gives the ability to emulate and create enumeration objects natively in PHP.
类摘要
SplEnum
class SplEnum extends SplType {
/* Constants */
const NULL
SplEnum::__default = null ;
/* 方法 */
public array
getConstList ([ <span
class="methodparam">bool
$include_default = false
] )
/* 继承的方法 */
SplType::__construct ([ <span
class="methodparam">mixed
$initial_value [, <span
class="type">bool $strict ]] )
}
预定义常量
SplEnum::__default
范例
示例 #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;
}
?>
以上例程会输出:
6
Value not a const in enum Month
SplEnum::getConstList
Returns all consts (possible values) as an array
说明
public array
SplEnum::getConstList ([ <span
class="methodparam">bool
$include_default = false
] )
Warning
本函数还未编写文档,仅有参数列表。
参数
include_default
Whether to include __default property.
返回值
范例
示例 #1 SplEnum::getConstList example
<?php
$bool = new SplBool;
var_dump($bool->getConstList(true));
?>
以上例程会输出:
array(3) {
["__default"]=>
bool(false)
["false"]=>
bool(false)
["true"]=>
bool(true)
}
简介
The SplBool class is used to enforce strong typing of the bool type.
类摘要
SplBool
class SplBool extends SplEnum {
/* Constants */
const bool
SplBool::__default = false ;
const bool
SplBool::false = false ;
const bool
SplBool::true = true ;
/* 继承的方法 */
public array
SplEnum::getConstList ([ <span
class="methodparam">bool
$include_default = false
] )
}
预定义常量
SplBool::__default
SplBool::false
SplBool::true
范例
示例 #1 SplBool usage example
<?php
$true = new SplBool(true);
if ($true) {
echo "TRUE\n";
}
$false = new SplBool;
if ($false) {
echo "FALSE\n";
}
?>
以上例程会输出:
TRUE
简介
The SplString class is used to enforce strong typing of the string type.
类摘要
SplString
class SplString <span class="ooclass"> extends SplType {
/* Constants */
const string
SplString::__default = '' ;
/* 继承的方法 */
SplType::__construct ([ <span
class="methodparam">mixed
$initial_value [, <span
class="type">bool $strict ]] )
}
预定义常量
SplString::__default
范例
示例 #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"
?>
以上例程会输出:
Value not a string
object(SplString)#1 (1) {
["__default"]=>
string(7) "Testing"
}
Testing