Wednesday, May 20, 2026
HomeLanguagesPHP | DsVector __construct() Function

PHP | Ds\Vector __construct() Function

The Ds\Vector::__construct() function is an inbuilt function in PHP which is used to creates a new instance.

Syntax:

public Ds\Vector::__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\Vector::__construct() function in PHP:

Program 1:




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


Output:

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

Program 2:




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


Output:

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

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

RELATED ARTICLES

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS