Silva
Silva

Reputation: 3

Why is this simple hello world PHP code not working?

class saySomething {

    var $helloWorld = 'hello world';

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

}

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

the above gives this error:

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

Upvotes: 0

Views: 357

Answers (6)

Matt
Matt

Reputation: 453

All of the above answers are functionally correct.

You asked "why" - the reason is due to the programming term 'scope'. Scope defines what variables are visible, and when they are visible. Your example code defines a class-level variable $helloWorld and also a class method which takes a parameter $helloWorld.

The only variable that is 'in scope' when the function executes is the one passed as a parameter. So, when the code later invokes that method without assigning a value to the parameter you get an error trying to output its value (since it doesn't have one). At this point the method can't see the class-level variable as it is not in scope.

A solution, as above, is to either pass a value to the function's parameter so that it is defined (and hence no error is generated)

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

Which would pass a value to the class method, and you would see 'Hello world... again' printed to the screen.

This may, or may not, be what you intended to do. If you wished to understand how to bring the class level variable into scope, then the most common way is to use the pre-defined PHP variable '$this' which allows the method to reference (i.e. "see") other variables and methods within the class. The variable '$this' auto-magically always refers to the current class wherever it is used.

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'

Upvotes: 1

Michael
Michael

Reputation: 7384

use the $this pointer

class saySomething {

    var $helloWorld = 'hello world';

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

}

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

Upvotes: 0

Vivek Goel
Vivek Goel

Reputation: 24150

you can modify it as

    class saySomething {

    var $helloWorld = 'hello world';

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

}

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

or your sayhelloworld doesn't require argument as helloWorld is already defined.

class saySomething {

var $helloWorld = 'hello world';

function sayHelloWorld()
{
    echo $helloWorld;
}

}

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

Upvotes: 0

mauris
mauris

Reputation: 43619

The correct code you probably want should be:

class saySomething {

    var $helloWorld = 'hello world';

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

}

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

On the line function sayHelloWorld($helloWorld) you wrote, you meant that $helloWorld is a parameter passed into the method/function. To access class variables, use $this->variableName instead.

Upvotes: 1

regality
regality

Reputation: 6554

class saySomething {

    var $helloWorld = 'hello world';

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

}

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

Upvotes: 0

awm
awm

Reputation: 6570

Because you're missing argument 1 for saySomething::sayHelloWorld(). When you defined the function, you defined it as having 1 required argument.

Define your function like this:

class saySomething {

    var $helloWorld = 'hello world';

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

}

Upvotes: 5

Related Questions