PHP - Ds\Stack::copy() Function
The PHP Ds\Stack::copy() function is used to create and retrieve a shallow copy of the stack.
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\Stack::copy() function −
public Ds\Stack::copy(): Ds\Stack
Parameters
This function does not accept any parameter.
Return value
This function returns a shallow copy of the stack.
Example 1
The following program demonstrates the usage of the PHP Ds\Stack::copy() function −
<?php $stack = new \Ds\Stack([10, 20, 30, 40, 50]); echo "The stack elements are: \n"; print_r($stack); echo "The shallow copy of the stack is: \n"; #using copy() function print_r($stack->copy()); ?>
Output
The above program produces the following output −
The stack elements are:
Ds\Stack Object
(
[0] => 50
[1] => 40
[2] => 30
[3] => 20
[4] => 10
)
The shallow copy of the stack is:
Ds\Stack Object
(
[0] => 50
[1] => 40
[2] => 30
[3] => 20
[4] => 10
)
Example 2
Following is another example of the PHP Ds\Stack::copy() function. We use this function to retrieve a shallow copy of this stack (["Tutorials", "Point", "India"]) −
<?php
$stack = new \Ds\Stack([]);
$stack->push("Tutorials", "Point", "India");
echo "The stack elements are: \n";
print_r($stack);
echo "The shallow copy of the stack is: \n";
#using copy() function
print_r($stack->copy());
?>
Output
After executing the above program, it will display the following output −
The stack elements are:
Ds\Stack Object
(
[0] => India
[1] => Point
[2] => Tutorials
)
The shallow copy of the stack is:
Ds\Stack Object
(
[0] => India
[1] => Point
[2] => Tutorials
)