个性化阅读
专注于IT技术分析

PHP如何使用Ds\Deque __construct()函数?用法实例

Ds\Deque::__ construct()函数是PHP中的内置函数, 用于创建新实例。

语法如下:

Ds\Deque::__construct( $values )

参数:该函数接受单个参数$值包含可遍历的对象或数组以使用初始值。

下面的程序说明了Ds\Deque::__ construct()PHP中的功能:

程序1:

<?php 
  
// Declare a deque 
$deq = new \Ds\Deque();
  
// Display the elements of deque
echo ( "Elements of first deque:\n" ); 
var_dump( $deq );
  
// Declare a deque 
$deq = new \Ds\Deque([10, 20, 30, 40, 50, 60]); 
  
// Display the elements of deque
echo ( "\nElements of second deque:\n" ); 
var_dump( $deq );
  
?>

输出如下:

Elements of first deque:
object(Ds\Deque)#1 (0) {
}

Elements of second deque:
object(Ds\Deque)#2 (6) {
  [0]=>
  int(10)
  [1]=>
  int(20)
  [2]=>
  int(30)
  [3]=>
  int(40)
  [4]=>
  int(50)
  [5]=>
  int(60)
}

程式2:

<?php 
  
// Declare a deque 
$deq = new \Ds\Deque([ "geeks" , "for" , "geeks" ]); 
  
// Display the elements of deque
echo ( "Elements of first deque:\n" ); 
print_r( $deq );
  
// Declare a deque 
$deq = new \Ds\Deque([ 'G' , 'E' , 'E' , 'K' , 'S' , 1, 2, 3]); 
  
// Display the elements of deque
echo ( "\nElements of second deque:\n" ); 
print_r( $deq );
  
?>

输出如下:

Elements of first deque:
Ds\Deque Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
)

Elements of second deque:
Ds\Deque Object
(
    [0] => G
    [1] => E
    [2] => E
    [3] => K
    [4] => S
    [5] => 1
    [6] => 2
    [7] => 3
)

参考: https://www.php.net/manual/en/ds-deque.construct.php


赞(0) 打赏
未经允许不得转载:srcmini » PHP如何使用Ds\Deque __construct()函数?用法实例
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏