What is the DS\Vector __construct() method in PHP?

Overview

The __construct() is an built-in magic method of the DS\Vector class in PHP that’s used to construct a new instance of a vector from a traversable object or array.

Prototype

public Ds\Vector::__construct($values = ?)

Parameters

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

Return value

It will return a new vector.

Example

<?php
$arr = new \Ds\Vector([10, 20, 30, 40, 50]);
print_r($arr);
$arr = new \Ds\Vector(['H', 'E', 'L', 'L', 'O']);
print_r($arr);
?>

Explanation

  • Line 3: We create a vector $arr of integer values.
  • Line 4: We print $arr.
  • Line 6: We create a vector $arr of characters.
  • Line 7: We print $arr.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved