PHP | Ds\Pair __construct() Function

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

Syntax:

public Ds\Pair::__construct( $key, $value )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $key: This parameter holds the key of the pair element.
  • $value: This parameter holds the value of the pair element.

Return Value: This function does not return any value.

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



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to illustrate the 
// Ds\Pair::__construct() function
  
// Declare a new pair 
$pair = new \Ds\Pair(); 
  
// Display the pair elements 
print_r($pair); 
  
// Creating another pair 
$pair = new \Ds\pair(
    ["1", "2", "3"],
    ["Geeks", "for", "Geeks"]
);
  
// Display the pair elements 
print_r($pair); 
  
?>

chevron_right


Output:

Ds\Pair Object
(
    [key] => 
    [value] => 
)
Ds\Pair Object
(
    [key] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [value] => Array
        (
            [0] => Geeks
            [1] => for
            [2] => Geeks
        )

)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to illustrate the 
// Ds\Pair::__construct() function
  
// Creating a pair 
$pair = new \Ds\pair(
    ["a", "b", "c"],
    ["10", "2", "30"]
);
  
// Display the pair elements 
var_dump($pair); 
  
?>

chevron_right


Output:

object(Ds\Pair)#1 (2) {
  ["key"]=>
  array(3) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
  }
  ["value"]=>
  array(3) {
    [0]=>
    string(2) "10"
    [1]=>
    string(1) "2"
    [2]=>
    string(2) "30"
  }
}

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.