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

PHP - Ds\Queue::copy() Function



The PHP Ds\Queue::copy() function is used to create a shallow copy of a queue. A "shallow" copy means that the properties of the copied queue share the same references as those of the original queue, rather than creating new instances of the elements.

The order of elements in the shallow copy will be the same as in the original queue.

Syntax

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

public Ds\Queue::copy(): Ds\Queue

Parameters

This function does not accept any parameters.

Return value

This function returns the shallow copy of a queue.

Example 1

The following program demonstrates the usage of the PHP Ds\Queue::copy() function −

<?php  
   $queue = new \Ds\Queue([10, 20, 30, 40, 50]);  
   echo "The original queue is: \n";
   print_r($queue);
   echo "The shallow copy or original queue: \n";
   #using copy() function
   print_r($queue->copy());   
?>

Output

Once the above programming is executed, the following output will be displayed −

The original queue is:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The shallow copy or original queue:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Example 2

Following is another example of the PHP Ds\Queue::copy() function. We use this function to retrieve a shallow copy of this queue (["Tutorials", "Point", "India"]) −

<?php  
   $queue = new \Ds\Queue(["Tutorials", "Point", "India"]); 
   $queue->push("Hyderabad");   
   echo "The original queue is: \n";
   print_r($queue);
   echo "The shallow copy or original queue: \n";
   #using copy() function
   print_r($queue->copy());
?>

Output

The above program produces the following output −

The original queue is:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
    [3] => Hyderabad
)
The shallow copy or original queue:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
    [3] => Hyderabad
)
php_function_reference.htm
Advertisements