Ds \ Vector :: allocate()function是PHP中的内置函数, 用于为所需容量分配足够的内存。它提供向量的自定义大小以分配空间。
语法如下:
void public Ds\Vector::allocate( $capacity )
参数:该函数接受单个参数$容量保留要分配的空间。
注意:如果此值小于或等于当前容量, 容量将保持不变。
返回值:此函数不返回任何值。
下面的程序说明了Ds \ Vector :: allocate()PHP中的功能:
程序1:
<?php
// Declare new vector
$vector = new \Ds\Vector();
echo ( "Allocated Space is: " );
// Use capacity() function
var_dump( $vector ->capacity());
echo ( "Allocated space is: " );
// Use allocate() function to
// allocate capacity
$vector ->allocate(50);
// Display the allocated vector
// capacity
var_dump( $vector ->capacity());
?>
输出如下:
Allocated Space is: int(8)
Allocated space is: int(50)
程式2:
<?php
// Declare new vector
$vector = new \Ds\Vector();
echo ( "Allocated Space is: " );
// Use capacity() function
var_dump( $vector ->capacity());
echo ( "Allocated space is: " );
// Use allocate() function to
// allocate capacity
$vector ->allocate(5);
// Display the Vector capacity
var_dump( $vector ->capacity());
// Use allocate() function to
// allocate capacity
$vector ->allocate(120);
// Display the Vector capacity
var_dump( $vector ->capacity());
?>
输出如下:
Allocated Space is: int(8)
Allocated space is: int(8)
int(120)
参考: http://php.net/manual/en/ds-vector.allocate.php