Loading, please wait...

A to Z Full Forms and Acronyms

How to demonstrate the parameterized constructor in PHP | PHP programs

Jan 24, 2022 PHP, 3140 Views
How to demonstrate the parameterized constructor in PHP | PHP programs

How to demonstrate the parameterized constructor in PHP | PHP programs

We will create a class with data members and implement a parameterized constructor to initialize data members.

<?php  

    class Sample
    {  
        private $num1;
        private $num2;
        
        public function Sample($n1, $n2)
        {  
            $this->num1=$n1;
            $this->num2=$n2;
        }  
        public function PrintValues()
        {  
            echo "Num1: ".$this->num1."<br>";  
            echo "Num2: ".$this->num2."<br>";  
        }  
    }  
    $S = new Sample(10,20);  
    $S->PrintValues();
?> 
A to Z Full Forms and Acronyms