PHP中的型別約束介紹
PHP的類方法和函式中可實現型別約束,但引數只能指定類、陣列、介面、callable 四種型別,引數可預設為NULL,PHP並不能約束標量型別或其它型別。
如下示例:
複製程式碼程式碼如下:
<?php
class Test
{
public function test_array(array $arr)
{
print_r($arr);
}
public function test_class(Test1 $test1 = null)
{
print_r($test1);
}
public function test_callable(callable $callback, $data)
{
call_user_func($callback, $data);
}
public function test_interface(Traversable $iterator)
{
print_r(get_class($iterator));
}
public function test_class_with_null(Test1 $test1 = NULL)
{
}
}
class Test1{}
$test = new Test();
//函式呼叫的引數與定義的引數型別不一致時,會丟擲一個可捕獲的致命錯誤。
$test->test_array(array(1));
$test->test_class(new Test1());
$test->test_callable('print_r', 1);
$test->test_interface(new ArrayObject(array()));
$test->test_class_with_null();
那麼對於標量型別如何約束呢?
PECL擴充套件庫中提供了SPL Types擴充套件實現interger、float、bool、enum、string型別約束。
複製程式碼程式碼如下:
$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
*/
SPL Types會降低一定的靈活性和效能,實際專案中三思而行。
PHP 5 可以使用型別約束。函式的引數可以指定只能為物件(在函式原型裡面指定類的名字),php 5.1 之後也可以指定只能為陣列。 注意,即使使用了型別約束,如果使用NULL作為引數的預設值,那麼在呼叫函式的時候依然可以使用NULL作為實參。
Example #1 型別約束示例
<?php
//如下面的類
class MyClass
{
/**
* 測試函式
* 第一個引數必須為類OtherClass的一個物件
*/
public function test(OtherClass $otherclass) {
echo $otherclass->var;
}
/**
* 另一個測試函式
* 第一個引數必須為陣列
*/
public function test_array(array $input_array) {
print_r($input_array);
}
}
//另外一個類
class OtherClass {
public $var = 'Hello World';
}
?>
函式呼叫的引數與定義的引數型別不一致時,會丟擲一個致命錯誤。
<?php
// 兩個類的物件
$myclass = new MyClass;
$otherclass = new OtherClass;
// 致命錯誤:引數1必須是類OtherClass的一個物件
$myclass->test('hello');
// 致命錯誤:引數1必須為類OtherClass的一個例項
$foo = new stdClass;
$myclass->test($foo);
// 致命錯誤:引數1不能為null
$myclass->test(null);
// 正確:輸出 Hello World
$myclass->test($otherclass);
// 致命錯誤:引數1必須為陣列
$myclass->test_array('a string');
// 正確:輸出 陣列
$myclass->test_array(array('a', 'b', 'c'));
?>
型別約束不只是用在類的成員函式裡,也能使用在函式裡。
<?php
// 如下面的類
class MyClass {
public $var = 'Hello World';
}
/**
* 測試函式
* 第一個引數必須是類MyClass的一個物件
*/
function MyFunction (MyClass $foo) {
echo $foo->var;
}
// 正確
$myclass = new MyClass;
MyFunction($myclass);
?>
型別約束允許NULL值:
<?php
/* 接受NULL值 */
function test(stdClass $obj = NULL) {
}
test(NULL);
test(new stdClass);
?>
型別約束只支援物件 和 陣列(php 5.1之後)兩種型別。而不支援整型 和 字串型別。
相關文章
- PHP7型別約束PHP型別
- 約束介紹
- SQLServer約束介紹SQLServer
- C# 泛型 引用型別約束 值型別約束C#泛型型別
- SQL教程——常見的約束型別SQL型別
- php中explode與split的區別介紹PHP
- TreeSet的null值與元素型別的約束Null型別
- 資料庫中欄位資料型別以及約束資料庫資料型別
- Oracle constraints type 約束型別OracleAI型別
- [譯]Kotlin泛型中何時該用型別形參約束?Kotlin泛型型別
- 泛型的約束理解泛型
- C++中的基本變數型別介紹C++變數型別
- 練習:NICK方案中PurchaseOrder新增CHECK型別的約束,要求ReceivedDate >= PODate型別
- SQL中給varchar2(5)資料型別合適的約束SQL資料型別
- 以太坊智慧合約 Solidity 的常用資料型別介紹Solid資料型別
- PHP 中 include 和 require 的概要及區別介紹PHPUI
- Oracle約束簡介Oracle
- Oracle 中LONG RAW BLOB CLOB型別介紹Oracle型別
- Java泛型(三):型別擦除帶來的約束與侷限性Java泛型型別
- oracle中的約束Oracle
- 智慧合約語言 Solidity 教程系列1 – 型別介紹Solid型別
- string型別介紹型別
- MySQL——表的約束,資料型別,增刪查改MySql資料型別
- 完整性約束的型別(Types of Integrity Constraints)型別AI
- Sqlserver中所有約束的型別,建立、修改與刪除SQLServer型別
- C#泛型約束C#泛型
- Go 泛型之泛型約束Go泛型
- 智慧合約語言Solidity教程系列2 - 地址型別介紹Solid型別
- postgreSQL 索引(二)型別介紹SQL索引型別
- 【Redis】資料型別介紹Redis資料型別
- http代理型別格式介紹HTTP型別
- Rust 資料型別介紹Rust資料型別
- Oracle資料型別介紹Oracle資料型別
- iOS核心動畫型別介紹iOS動畫型別
- C#中泛型約束(where)是什麼?C#泛型
- 常見的代理IP型別介紹型別
- 介紹PostgreSQL的陣列型別FUSQL陣列型別
- Mycat的負載均衡型別介紹負載型別