PHP - Ds\Collection::copy() Function
The PHPDs\Collection::copy() function is used to create a shallow copy of a current collection such as vector, set, etc.
When this function is invoked on a collection, a shallow copy of that collection is created and the same shallow copy is returned in the output.
A shallow copy of a collection 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\Collection::copy() function −
abstract public Ds\Collection Ds\Collection::copy( void )
Parameters
This function does not accept any parameter.
Return value
This function returns a collection, which is a shallow copy of the current collection.
Example 1
The following program demonstrates the usage of the PHP Ds\Collection::copy() function −
<?php $collection = new \Ds\Vector([1, 2, 3, 4, 5]); echo "The original collection elements: \n"; print_r($collection); #using copy() function $res = $collection->copy(); echo "The shallow copy of this original collection is: \n"; var_dump($res); ?>
Output
The above program produces the following output −
The original collection elements:
Ds\Vector Object
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
The shallow copy of this original collection is:
object(Ds\Vector)#2 (5) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
[4]=>
int(5)
}
Example 2
Following is another example of the PHP Ds\Collection::copy() function. We use this function to create a shallow copy of this vector(collection) −
<?php $vect = new \Ds\Vector(); echo "Before adding elements: \n"; print_r($vect); $vect->push(121); $vect->push(272); $vect->push(31); $vect->push(26); $vect->push(99); $vect->push(81); echo "After adding elements: \n"; print_r($vect); #using copy() function $res = $vect->copy(); echo "The shallow copy of this original collection is: \n"; print_r($res); ?>
Output
After executing the above program, it generates the following output −
Before adding elements:
Ds\Vector Object
(
)
After adding elements:
Ds\Vector Object
(
[0] => 121
[1] => 272
[2] => 31
[3] => 26
[4] => 99
[5] => 81
)
The shallow copy of this original collection is:
Ds\Vector Object
(
[0] => 121
[1] => 272
[2] => 31
[3] => 26
[4] => 99
[5] => 81
)
Example 3
Let's create another collection named set([]) and use the samecopy()function to create a shallow of this set (['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']) −
<?php
$set = new \Ds\Set();
$set->add('Sunday');
$set->add('Monday');
$set->add('Tuesday');
$set->add('Wednesday');
$set->add('Thursday');
$set->add('Friday');
$set->add('Saturday');
echo "Original set elements are: \n";
print_r($set);
#using copy() function
$res = $set->copy();
echo "The shallow copy of this original collection is: \n";
print_r($res);
?>
Output
Once the above program is executed, it will generate the following output −
Original set elements are:
Ds\Set Object
(
[0] => Sunday
[1] => Monday
[2] => Tuesday
[3] => Wednesday
[4] => Thursday
[5] => Friday
[6] => Saturday
)
The shallow copy of this original collection is:
Ds\Set Object
(
[0] => Sunday
[1] => Monday
[2] => Tuesday
[3] => Wednesday
[4] => Thursday
[5] => Friday
[6] => Saturday
)
Example 4
In the example below, we use the PHP Ds\Collection::copy() to create a shallow copy to this vector (collection) (["test_string", 1525, false]) −
<?php $seq = new \Ds\Vector(["test_string", 1525, false]); echo "Original vector elements are: \n"; print_r($seq); $res = $seq->copy(); echo "The shallow copy of this original collection is: \n"; var_dump($res); ?>
Output
On executing the above program, it displays the following output:
Original vector elements are:
Ds\Vector Object
(
[0] => test_string
[1] => 1525
[2] =>
)
The shallow copy of this original collection is:
object(Ds\Vector)#2 (3) {
[0]=>
string(11) "test_string"
[1]=>
int(1525)
[2]=>
bool(false)
}