Wednesday, June 17, 2026
HomeLanguagesPHP | DsStack __construct() Function

PHP | Ds\Stack __construct() Function

The Ds\Stack::__construct() function is an inbuilt function in PHP which is used to create a new instance of stack.

Syntax:

public Ds\Stack::__construct( $values )

Parameters: This function accepts single parameter $values which holds the traversable object or array to use initial values.

Below programs illustrate the Ds\Stack::__construct() function in PHP:

Program 1:




<?php 
  
// Declare a new stack 
$stack = new \Ds\Stack(); 
print_r($stack); 
  
// Declare a new stack 
$stack = new \Ds\Stack(['G', 'E', 'K', 'S']); 
print_r($stack); 
  
?> 


Output:

Ds\Stack Object
(
)
Ds\Stack Object
(
    [0] => S
    [1] => K
    [2] => E
    [3] => G
)

Program 2:




<?php 
  
// Declare a new stack 
$stack = new \Ds\Stack([2, 3, 6, 7, 8]); 
var_dump($stack); 
  
// Declare a new stack 
$stack = new \Ds\Stack([2, 3, 5, 8, 9, 10]); 
var_dump($stack); 
  
?> 


Output:

object(Ds\Stack)#1 (5) {
  [0]=>
  int(8)
  [1]=>
  int(7)
  [2]=>
  int(6)
  [3]=>
  int(3)
  [4]=>
  int(2)
}
object(Ds\Stack)#2 (6) {
  [0]=>
  int(10)
  [1]=>
  int(9)
  [2]=>
  int(8)
  [3]=>
  int(5)
  [4]=>
  int(3)
  [5]=>
  int(2)
}

Reference: https://www.php.net/manual/en/ds-stack.construct.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS