PHP - Ds Vector::copy() 函式



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

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

語法

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

public Ds\Vector Ds\Vector::copy( void )

引數

此函式不接受任何引數。

返回值

此函式返回向量的淺複製。

示例 1

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

<?php 
   $vector = new \Ds\Vector([1, 2, 3, 5]);
   echo "The original vector: \n";
   print_r($vector);
   echo "The shallow of vector is: \n";
   #using copy() function
   print_r($vector->copy());
?>  

輸出

以上程式產生以下輸出:

The original vector:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
)
The shallow of vector is:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 5
)

示例 2

以下是 PHP Ds\Vector::copy() 函式的另一個示例。我們使用此函式來獲取此向量的淺複製(["Tutorials", "Point", "Tutorix"]):

<?php 
   $vector = new \Ds\Vector(["Tutorials", "Point", "Tutorix"]); 
   print_r($vector);
   echo "The shallow copy of vector is: \n";
   #using copy() function
   print_r($vector->copy());
?>

輸出

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

Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
The shallow copy of vector is:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => Tutorix
)
php_function_reference.htm
廣告