skip to Main Content

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 the ‘htdocs’ directory, then in that file defined a class with variables and a function, that initiates these variables, then below the class definition, I’m creating an instance of the class and call its function. Then I started Apache from the XAMPP control panel. When I open this PHP file in the browser as http://localhost/problem.php, the browser shows this error:

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

The function is public, so there are no problems with brackets in the 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:

<?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);

?>

3

Answers


  1. 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.

    Login or Signup to reply.
  2. In PHP, to call a function of an object, use "->" and not "."

    $np->setParams(1,2,1910,true);
    
    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search