Home > Enterprise >  How to solve "call to undefined function ..." problem in PHP 7.4.5, XAMPP 3.2.4, Windows 1
How to solve "call to undefined function ..." problem in PHP 7.4.5, XAMPP 3.2.4, Windows 1

Time:09-27

I use PHP 7.4.5 and Apache 2.0 on XAMPP 3.2.4 on Windows 10 x64. PHP API version is 20190902.

I created a .php file in 'htdocs' directory, then in that file defined a class with variables and a function, that initates these variables, then below the class definition I'm creating an instance of the class and call its function. Then I started Apache from XAMPP control panel. When I open this php file in browser as http://localhost/problem.php , the browser shows this error:

Uncaught Error: Call to undefined function setParams() in D:\xampp\htdocs\problem.php:34 Stack trace: #0 {main} thrown in D:\xampp\htdocs\problem.php on line 34

The function is public, so there are no problems with brackets in class description, no scope problem, so I can't figure out where this problem comes from. Do you have any idea?

P.S. I've already restarted Apache, but the result is still the same.

Here is the code of the problem.php:


    phpinfo();

    abstract class Person {

        var $name; // = string;
    }

    class NaturalPerson extends Person {

        var $dayOfBirth; // = integer;

        var $MonthOfBirth; // = integer;

        var $YearOfBirth; // = integer;

        var $male; // = boolean;

        public function setParams ($d, $m, $y, $ml) {

            $this->dayOfBirth = $d;

            $this->MonthOfBirth = $m;

            $this->YearOfBirth = $y;

            $this->male = $ml;
        }
    }

    $np = new NaturalPerson();

    $np.setParams(1,2,1910,true);

    echo "<br>\n";

    var_dump($np);

?>

CodePudding user response:

You're wrong in the call function. You should update:

$np.setParams(1,2,1910,true); -> $np->setParams(1,2,1910,true);

You can figure out more here.

CodePudding user response:

In PHP, to call a function of an object, use "->" and not "."

$np->setParams(1,2,1910,true);

CodePudding user response:

As others pointed out, you are using a string concatenation operator to call the member function on an object. What happens: PHP calls __toString() on $np (because it's an object) and append the returned value of the "setParams()" function to that of the string serialization of your object.

  • Related