內(nèi)部的、abstract final class Shapes提供了一些靜態(tài)方法,可以對任何類型的形狀進行操作。這些方法描述如下:
public static function idx(S $ shape,arraykey $ index):?Tv;
public static function idx(S $ shape,arraykey $ index,Tv $ default):Tv;
此方法搜索名為的字段的形狀$shape
(其類型在此指定為S
)$index
。如果該字段存在,則返回其值; 否則返回默認值。對于類型T的字段,該函數(shù)返回類型?
T的值。$default
可以提供默認值; 但是,如果省略該參數(shù),null
則使用該值。$index
必須是單引號的字符串或類常量的類型string
或int
。
<?hh
namespace Hack\UserDocumentation\Shapes\Functions\Examples\fIdx;
class C {
const string KEYX = 'x';
const string KEYY = 'y';
const int KEYINTX = 10;
const int KEYINTY = 23;
const int KEYINTZ = 50;
}
function run(): void {
echo "======== Shapes::idx ========\n";
$s = shape('x' => 10, 'y' => 20);
$v = Shapes::idx($s, 'x'); // field exists, return 10
echo "\$v = " . (($v == null)? "null" : $v) ."\n";
$v = Shapes::idx($s, 'y'); // field exists, return 20
echo "\$v = " . (($v == null)? "null" : $v) ."\n";
// field does not exist; return implict default, null
$v = Shapes::idx($s, 'z');
echo "\$v = " . (($v == null)? "null" : $v) ."\n";
// field does not exist; return explicit default, -99
$v = Shapes::idx($s, 'z', -99);
echo "\$v = " . (($v == null)? "null" : $v) ."\n";
echo "----------------------------\n";
$s = shape(C::KEYINTX => 10, C::KEYINTY => 20);
$v = Shapes::idx($s, C::KEYINTX); // field exists, return 10
echo "\$v = " . (($v == null)? "null" : $v) ."\n";
$v = Shapes::idx($s, C::KEYINTY); // field exists, return 20
echo "\$v = " . (($v == null)? "null" : $v) ."\n";
// field does not exist; return implict default, null
$v = Shapes::idx($s, C::KEYINTZ);
echo "\$v = " . (($v == null)? "null" : $v) ."\n";
// field does not exist; return explicit default, -99
$v = Shapes::idx($s, C::KEYINTZ, -99);
echo "\$v = " . (($v == null)? "null" : $v) ."\n";
}
run();
Output
======== Shapes::idx ========
$v = 10
$v = 20
$v = null
$v = -99
----------------------------
$v = 10
$v = 20
$v = null
$v = -99
public static function keyExists(S $shape, arraykey $index): bool;
此方法搜索名為的字段的形狀$shape
(其類型在此指定為S
)$index
。如果該字段存在,true
則返回; 否則false
返回。$index
必須是單引號的字符串或類常量的類型string
或int
。
<?hh
namespace Hack\UserDocumentation\Shapes\Functions\Examples\fKeyExists;
class C {
const string KEYX = 'x';
const string KEYY = 'y';
const int KEYINTX = 10;
const int KEYINTY = 23;
const int KEYINTZ = 50;
}
function run(): void {
echo "\n======== Shapes::keyExists ========\n\n";
$s = shape('id' => "23456", 'url' => "www.example.com", 'count' => 23);
$v = Shapes::keyExists($s, 'url'); // field exists, return true
echo "keyExists(\$s, 'x') = " . $v ."\n";
$v = Shapes::keyExists($s, 'name'); // does not exist, return false
echo "keyExists(\$s, 'name') = " . $v ."\n";
}
run();
Output
======== Shapes::keyExists ========
keyExists($s, 'x') = 1
keyExists($s, 'name') =
public static function removeKey(S $ shape,arraykey $ index):void;
給定一個形狀$shape
(其類型指定為此S
)和一個字段名稱$index
,此方法將從該形狀中刪除指定的字段。如果指定的字段不存在,則刪除請求將被忽略。$index
必須是單引號的字符串或類常量的類型string
或int
。
<?hh
namespace Hack\UserDocumentation\Shapes\Functions\Examples\fRemoveKey;
class C {
const string KEYX = 'x';
const string KEYY = 'y';
const int KEYINTX = 10;
const int KEYINTY = 23;
const int KEYINTZ = 50;
}
function run(): void {
echo "\n======== Shapes::removeKey ========\n\n";
$s = shape();
var_dump($s);
Shapes::removeKey($s, 'name'); // no such field, so request ignored
$a = Shapes::toArray($s);
echo "# elements in array = " . count($a) . "\n";
var_dump($s, $a);
echo "----------------------------\n";
$s = shape('x' => 10, 'y' => 20);
var_dump($s);
Shapes::removeKey($s, C::KEYX); // field 'x' removed
$a = Shapes::toArray($s);
echo "# elements in array = " . count($a) . "\n";
var_dump($s, $a);
echo "----------------------------\n";
$s = shape('id' => "23456", 'url' => "www.example.com", 'count' => 23);
var_dump($s);
Shapes::removeKey($s, 'url'); // field 'url' removed
$a = Shapes::toArray($s);
echo "# elements in array = " . count($a) . "\n";
var_dump($s, $a);
}
run();
Output
======== Shapes::removeKey ========
array(0) {
}
# elements in array = 0
array(0) {
}
array(0) {
}
----------------------------
array(2) {
["x"]=>
int(10)
["y"]=>
int(20)
}
# elements in array = 1
array(1) {
["y"]=>
int(20)
}
array(1) {
["y"]=>
int(20)
}
----------------------------
array(3) {
["id"]=>
string(5) "23456"
["url"]=>
string(15) "www.example.com"
["count"]=>
int(23)
}
# elements in array = 2
array(2) {
["id"]=>
string(5) "23456"
["count"]=>
int(23)
}
array(2) {
["id"]=>
string(5) "23456"
["count"]=>
int(23)
}
public static function toArray(S $shape): array<arraykey, mixed>;
此方法返回一個類型的數(shù)組,array<arraykey, mixed>
其中包含形狀中每個字段的一個元素$shape
。每個元素的鍵和值分別是相應字段的名稱和值。數(shù)組中元素的順序與字段插入形狀的順序相同。
<?hh
namespace Hack\UserDocumentation\Shapes\Functions\Examples\fToArray;
class C {
const string KEYX = 'x';
const string KEYY = 'y';
const int KEYINTX = 10;
const int KEYINTY = 23;
const int KEYINTZ = 50;
}
function run(): void {
echo "\n======== Shapes::toArray ========\n\n";
$s = shape();
$a = Shapes::toArray($s); // returns an array of 0 elements
echo "# elements in array = " . count($a) . "\n";
var_dump($s, $a);
echo "----------------------------\n";
$s = shape('x' => 10, 'y' => 20);
$a = Shapes::toArray($s);
echo "# elements in array = " . count($a) . "\n";
var_dump($s, $a);
echo "----------------------------\n";
$s = shape('y' => 20, 'x' => 10);
$a = Shapes::toArray($s);
echo "# elements in array = " . count($a) . "\n";
var_dump($s, $a);
echo "----------------------------\n";
$s = shape('id' => "23456", 'url' => "www.example.com", 'count' => 23);
// returns an array of 3 elements, of type string, string, and int, respectively
$a = Shapes::toArray($s);
echo "# elements in array = " . count($a) . "\n";
}
run();
Output
======== Shapes::toArray ========
# elements in array = 0
array(0) {
}
array(0) {
}
----------------------------
# elements in array = 2
array(2) {
["x"]=>
int(10)
["y"]=>
int(20)
}
array(2) {
["x"]=>
int(10)
["y"]=>
int(20)
}
----------------------------
# elements in array = 2
array(2) {
["y"]=>
int(20)
["x"]=>
int(10)
}
array(2) {
["y"]=>
int(20)
["x"]=>
int(10)
}
----------------------------
# elements in array = 3
更多建議: