PHP - Ds Stack::__construct() Function



The PHP Ds\Stack::__construct() function is used to create a new instance of a stack. This new instance refers to an object of the Ds\Stack class.

Syntax

Following is the syntax of the PHP Ds\Stack::__construct() function −

public Ds\Stack::__construct(mixed $values = ?)

Parameters

Following is the parameter of this function −

  • values − A traversable object or an array to use for the initial values.

Return value

This function does not return any value.

Example 1

The following is the basic example of the PHP Ds\Stack::__construct() function −

<?php
   $stack = new \Ds\Stack();
   print_r($stack);
   # declare another stack
   $stack  = new \DS\Stack([10, 20, 30]);
   print_r($stack);
?>

Output

The above program produces the following output −

Ds\Stack Object
(
)
Ds\Stack Object
(
    [0] => 30
    [1] => 20
    [2] => 10
)

Example 2

Following is another example of the PHP Ds\Stack::__construct() function. We use this function to create new instances −

<?php
   $stack = new \Ds\Stack(['a', 'e', 'i']);
   print_r($stack);
   # declare another stack
   $stack  = new \DS\Stack(['a', 'e', 'i', 'o', 'u']);
   print_r($stack);
?>

Output

After executing the above program, the following output will be displayed −

Ds\Stack Object
(
    [0] => i
    [1] => e
    [2] => a
)
Ds\Stack Object
(
    [0] => u
    [1] => o
    [2] => i
    [3] => e
    [4] => a
)
php_function_reference.htm
Advertisements