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



PHP 的Ds\Stack::copy()函式用於建立和檢索堆疊的淺複製。

集合(或集)的淺複製是指屬性與從中建立複製的源物件的屬性共享相同引用的複製。

語法

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

public Ds\Stack::copy(): Ds\Stack

引數

此函式不接受任何引數。

返回值

此函式返回堆疊的淺複製。

示例 1

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

<?php 
   $stack = new \Ds\Stack([10, 20, 30, 40, 50]);
   echo "The stack elements are: \n";
   print_r($stack);
   echo "The shallow copy of the stack is: \n";
   #using copy() function
   print_r($stack->copy());
?>

輸出

以上程式產生以下輸出:

The stack elements are:
Ds\Stack Object
(
    [0] => 50
    [1] => 40
    [2] => 30
    [3] => 20
    [4] => 10
)
The shallow copy of the stack is:
Ds\Stack Object
(
    [0] => 50
    [1] => 40
    [2] => 30
    [3] => 20
    [4] => 10
)

示例 2

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

<?php 
   $stack = new \Ds\Stack([]);
   $stack->push("Tutorials", "Point", "India");
   echo "The stack elements are: \n";
   print_r($stack);
   echo "The shallow copy of the stack is: \n";
   #using copy() function
   print_r($stack->copy());
?>

輸出

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

The stack elements are:
Ds\Stack Object
(
    [0] => India
    [1] => Point
    [2] => Tutorials
)
The shallow copy of the stack is:
Ds\Stack Object
(
    [0] => India
    [1] => Point
    [2] => Tutorials
)
php_function_reference.htm
廣告