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.
PHP Code:
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?
PHP Code:
$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.
Bookmarks