PHP - Ds Vector::__construct() 函式



PHP 的 Ds\Vector::__construct() 函式用於建立一個新的向量例項。此新例項引用 Ds\Vector 類的物件。

語法

以下是 PHP Ds\Vector::__construct() 函式的語法:

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

引數

以下是此函式的引數:

  • values - 可遍歷的物件或用於初始值的陣列。

返回值

此函式不返回值。

示例 1

以下是 PHP Ds\Vector::__construct() 函式的基本示例:

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

輸出

以上程式產生以下輸出:

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

示例 2

以下是 PHP Ds\Vector::__construct() 函式的另一個示例。我們使用此函式來建立新例項:

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

輸出

執行以上程式後,將顯示以下輸出:

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