Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个简单的hello world PHP代码不起作用?_Php - Fatal编程技术网

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

为什么这个简单的hello world PHP代码不起作用?,php,Php,上面给出了这个错误: class saySomething { var $helloWorld = 'hello world'; function sayHelloWorld($helloWorld) { echo $helloWorld; } } $saySomething = new saySomething(); $saySomething->sayHelloWorld(); 因为您缺少saySomething::sayHel

上面给出了这个错误:

class saySomething {

    var $helloWorld = 'hello world';

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

}

$saySomething = new saySomething();
$saySomething->sayHelloWorld();

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

如下定义您的函数:

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

您可能需要的正确代码应该是:

class saySomething {

    var $helloWorld = 'hello world';

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

}

$saySomething = new saySomething();
$saySomething->sayHelloWorld();
在您编写的
函数sayHelloWorld($helloWorld)
行中,您的意思是
$helloWorld
是传递给方法/函数的参数。要访问类变量,请改用
$this->variableName

您可以将其修改为

class saySomething {

    var $helloWorld = 'hello world';

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

}

$saySomething = new saySomething();
$saySomething->sayHelloWorld();
或者您的sayhelloworld不需要参数,因为helloWorld已经定义

课堂上说了些什么{

    class saySomething {

    var $helloWorld = 'hello world';

    function sayHelloWorld($helloWorld="default value if no argument")
    {
        echo $helloWorld;
    }

}

$saySomething = new saySomething();
$saySomething->sayHelloWorld();
}

$saySomething=新saySomething(); $saySomething->sayHelloWorld()

使用$this指针

var $helloWorld = 'hello world';

function sayHelloWorld()
{
    echo $helloWorld;
}

以上所有答案在功能上都是正确的

您问“为什么”-原因是由于编程术语“范围”。范围定义了哪些变量是可见的,以及它们何时可见。您的示例代码定义了一个类级变量$helloWorld,以及一个接受参数$helloWorld的类方法

函数执行时唯一“在范围内”的变量是作为参数传递的变量。因此,当代码稍后调用该方法而不为参数赋值时,您会在尝试输出其值时收到一个错误(因为它没有值)。此时,该方法无法看到类级别变量,因为它不在范围内

如上所述,解决方案是将值传递给函数的参数,以便对其进行定义(因此不会生成错误)

这将向类方法传递一个值,您将看到“Hello world…”。。。再次“打印到屏幕上”

这可能是你想要做的,也可能不是。如果您希望了解如何将类级别变量引入范围,那么最常见的方法是使用预定义的PHP变量“$this”,它允许方法引用(即“see”)类中的其他变量和方法。变量“$this”自动神奇地始终引用当前类,无论它在何处使用

$saySomething = new saySomething();
$saySomething->sayHelloWorld('Hello world... again');

sayHelloWorld()缺少参数如果您从hello world教程中获得此代码,那么您应该立即找到另一个PHP教程。
class saySomething {

    var $helloWorld = 'hello world';

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

}

$saySomething = new saySomething();
$saySomething->sayHelloWorld();
$saySomething = new saySomething();
$saySomething->sayHelloWorld('Hello world... again');
class saySomething {

    var $helloWorld = 'hello world';

    function sayHelloWorld($helloWorld)
    {
        //set myOutput to parameter value (if set), otherwise value of class var
        $myOutput = (isset($helloWorld)) ? $helloWorld : $this->helloWorld;
        echo $myOutput;
    }

}

$saySomething = new saySomething();
$saySomething->sayHelloWorld(); // Outputs 'hello world' from class definition
$saySomething->sayHelloWorld('Hello world... again'); // Outputs 'Hello world... again'