• PHP Video Tutorials

PHP - Ds Set copy() Function



The PHP Ds\Set::copy() is used to retrieve a shallow copy of the current set. You can invoke this function on any collection such as a set, stack, deque, etc.

A shallow copy of a collection (or set) 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\Set::copy() function −

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

Parameters

This function does not accept any parameter.

Return value

This function returns a shallow copy of the given set.

Example 1

The following program demonstrates the usage of the PHP Ds\Set::copy() function.

<?php
   $set = new \Ds\Set(["She", "is", "eating", "a", "mango"]);
   echo "The set elements are: \n";
   print_r($set);
   echo "The shallow copy of set: \n";
   #using copy() function
   $set_copy = $set -> copy();
   print_r($set_copy);
?>

Output

The above program produces the following output −

The set elements are:
Ds\Set Object
(
    [0] => She
    [1] => is
    [2] => eating
    [3] => a
    [4] => mango
)
The shallow copy of set:
Ds\Set Object
(
    [0] => She
    [1] => is
    [2] => eating
    [3] => a
    [4] => mango
)

Example 2

The following is another example of the PHP Ds\Set::copy() function. We use this function to retrieve a shallow copy of this set ([101,102,103,104,105]).

<?php
   $set = new \Ds\Set([101, 102, 103, 104, 105]);
   echo "The set elements are: \n";
   print_r($set);
   echo "The shallow copy of a set: ";
   #using copy() function
   $set_copy = $set -> copy();
   print_r($set_copy);
?>

Output

After executing the above program, it will display the following output −

The set elements are:
Ds\Set Object
(
    [0] => 101
    [1] => 102
    [2] => 103
    [3] => 104
    [4] => 105
)
The shallow copy of a set: Ds\Set Object
(
    [0] => 101
    [1] => 102
    [2] => 103
    [3] => 104
    [4] => 105
)
php_function_reference.htm
Advertisements