为什么这个简单的hello world PHP代码不起作用?

0 php

class saySomething {

    var $helloWorld = 'hello world';

    function sayHelloWorld($helloWorld)
    {
        echo $helloWorld;
    }

}

$saySomething = new saySomething();
$saySomething->sayHelloWorld();
Run Code Online (Sandbox Code Playgroud)

上面给出了这个错误:

Warning: Missing argument 1 for saySomething::sayHelloWorld(), called in C:\xampp\htdocs\test.php on line 15 and defined in C:\xampp\htdocs\test.php on line 7
Run Code Online (Sandbox Code Playgroud)

awm*_*awm 5

因为你缺少saySomething :: sayHelloWorld()的参数1.定义函数时,将其定义为具有1个必需参数.

像这样定义你的函数:

class saySomething {

    var $helloWorld = 'hello world';

    function sayHelloWorld()
    {
        echo $this->helloWorld;
    }

}
Run Code Online (Sandbox Code Playgroud)