Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Vector::copy() Function



The PHP Ds\Vector::copy() function is used to retrieve a shallow copy of a vector. The order of elements in the returned copy will be the same as in the original vector.

A shallow copy of a collection (or vector) is a copy where the properties share the same references as those of the source object from which the copy was made.

Syntax

Following is the syntax of the PHP Ds\Vector::copy() function −

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

Parameters

This function does not accept any parameter.

Return value

This function returns a shallow copy of the vector.

Example 1

The following is the basic example of the PHP Ds\Vector::copy() function −

<?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());
?>  

Output

The above program produces the following output −

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
)

Example 2

Following is another example of the PHP Ds\Vector::copy() function. We use this function to retrieve a shallow copy of this vector (["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());
?>

Output

After executing the above program, the following output will be displayed −

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
Advertisements