How to call a class(echo)

hi folks i am new at OOP, i created this simple class. its not printing. what am i doing wrong?

class test{
    function world(){
        echo "Hello World";    
    }
}
$testing = new test;
$testing->world()

There’s a ; missing at the end of the last line.

Do you get an error?

Not i don’t get any error.:confused: i put the ; also

add
error_reporting(E_ALL);
To the top of the script.
Then add something like:
echo “Script is done”;
At the bottom just to make sure the script is actually executing.

It was a port error. my ports were conflicting. now its working. although i i wanna know what these two steps do in simple english

$testing = new test;
$testing->world()  

line 1 instantiates an object (fires it up)
line 2 tells the object to now fire up a function, called world

So you are in fact now delegating some responsibility to that new object you called “testing”.

The blueprint for this object was defined in your class.

Generally speaking though, in your blueprint you tell your methods (functions) to return stuff rather than echo things.


class test{
    function world(){
        return "Hello World";    
    }
}

Because who is to say that in all future cases that when you instantiate the object, and tell it to do something, that you want it to echo the result onto the page in that exact place?


$testing = new test;
echo $testing->world();

// OR


$testing = new test;
$msg = $testing->world();

// then maybe miles away ...

echo '<h1>' . $msg . '<h1>' . PHP_EOL;

By having your class echo things it can be thought of as “taking on too much responsibility”, so, delegating authority, assigning responsibility these are typical design issues you will need to grapple with in order to gain a better understanding of OOP.

Sorry for the long post, just wanted to see what this auto-save discussion was about. Seems nice.