PHP - Ds\Pair::copy() Function



The PHP Ds\Pair::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 pair.

A shallow copy of a pair 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\Pair::copy() function −

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

Parameters

This function does not accept any parameter.

Return value

This function returns a shallow copy of a pair.

Example 1

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

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

Output

The above program produces the following output −

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] =>
)

Example 2

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

Output

After executing the above program, the following output is displayed −

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
Advertisements