PHP - Ds\Queue::copy() 函式



PHP 的Ds\Queue::copy()函式用於建立佇列的淺複製。“淺”複製意味著被複制佇列的屬性與原始佇列共享相同的引用,而不是建立元素的新例項。

淺複製中元素的順序與原始佇列中的順序相同。

語法

以下是 PHP Ds\Queue::copy() 函式的語法:

public Ds\Queue::copy(): Ds\Queue

引數

此函式不接受任何引數。

返回值

此函式返回佇列的淺複製。

示例 1

以下程式演示了 PHP Ds\Queue::copy() 函式的使用:

<?php  
   $queue = new \Ds\Queue([10, 20, 30, 40, 50]);  
   echo "The original queue is: \n";
   print_r($queue);
   echo "The shallow copy or original queue: \n";
   #using copy() function
   print_r($queue->copy());   
?>

輸出

執行上述程式後,將顯示以下輸出:

The original queue is:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The shallow copy or original queue:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

示例 2

以下是 PHP Ds\Queue::copy() 函式的另一個示例。我們使用此函式檢索此佇列(["Tutorials", "Point", "India"]) 的淺複製:

<?php  
   $queue = new \Ds\Queue(["Tutorials", "Point", "India"]); 
   $queue->push("Hyderabad");   
   echo "The original queue is: \n";
   print_r($queue);
   echo "The shallow copy or original queue: \n";
   #using copy() function
   print_r($queue->copy());
?>

輸出

上述程式產生以下輸出:

The original queue is:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
    [3] => Hyderabad
)
The shallow copy or original queue:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
    [3] => Hyderabad
)
php_function_reference.htm
廣告