I am getting an error that I have to few arguments in my function?

Viewed 19

im writing a class in php and I am getting an error stating:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function StudentAccountInquiry::get_semester(), 0 passed in /home/cg/root/632dc36089450/main.php on line 28 and exactly 1 expected in /home/cg/root/632dc36089450/main.php:14
Stack trace:
#0 /home/cg/root/632dc36089450/main.php(28): StudentAccountInquiry->get_semester()
#1 {main} thrown in /home/cg/root/632dc36089450/main.php on line 14

<?php
class StudentAccountInquiry{
    public $semester;
    public $balance;
    
    function set_semester($semester){
        $this->semester = $semester;
    }

    function set_balance($balance){
        $this->balance = $balance;
    }

    function get_semester($semester){
        return $this->semester;
    }
    
    function get_balance($balance){
        return $this->balance;
    }

}

$fall = new StudentAccountInquiry();
$fallBalance = new StudentAccountInquiry();
$fall->set_semester('Fall 2022');
$fallBalance->set_balance('$10,000');
echo $fall->get_semester();
echo $balance->get_balance();
?>

It looks to me that everything is there.

Please Advise, Thanks!

1 Answers

get_balance and get_balance function has function arguments $semester and $balance respectively , which you actually do not need, as you just returning class variable so you can change for function definition to this

Old

function get_semester($semester){
    return $this->semester;
}

function get_balance($balance){
    return $this->balance;
}

New

function get_semester(){
    return $this->semester;
}

function get_balance(){
    return $this->balance;
}
Related