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



PHP 的Ds\Pair::copy()函式用於檢索向量的淺複製。返回的複製中的元素順序與原始對中的順序相同。

對的淺複製是指屬性與建立該複製的源物件的屬性共享相同引用的複製。

語法

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

public Ds\Pair::copy(): Ds\Pair

引數

此函式不接受任何引數。

返回值

此函式返回對的淺複製。

示例 1

以下是 PHP Ds\Pair::copy()函式的基本示例:

<?php  
   $pair = new \Ds\Pair([1, 2, 3, 4, 5]);
   echo "The pair elements are: \n";
   print_r($pair);
   echo "The shallow copy of a pair is: \n";
   print_r($pair-<copy());
?>

輸出

上述程式產生以下輸出:

The pair elements are:
Ds\Pair Object
(
    [key] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )

    [value] =>
)
The shallow copy of a pair is:
Ds\Pair Object
(
    [key] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )

    [value] =>
)

示例 2

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

<?php  
   $pair = new \Ds\Pair(["Tutorial", "Point", "India"]);
   echo "The pair elements are: \n";
   print_r($pair);
   echo "The shallow copy of a pair is: \n";
   print_r($pair->copy());
?>

輸出

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

The pair elements are:
Ds\Pair Object
(
    [key] => Array
        (
            [0] => Tutorial
            [1] => Point
            [2] => India
        )

    [value] =>
)
The shallow copy of a pair is:
Ds\Pair Object
(
    [key] => Array
        (
            [0] => Tutorial
            [1] => Point
            [2] => India
        )

    [value] =>
)
php_function_reference.htm
廣告