PHP: Indirect method access does not work parent:?

Ar
11

I'm trying to make the code in my book work.

Following code:

class computer {
public $ CPU = "The CPU";

start public function () {
echo "computer starts.
";
}

Shut down public function ($ seconds) {
if (is_integer ($ seconds)) {
$ seconds = $ seconds. "Seconds";
}

echo "Computer goes down in". $ Seconds. "
";
}
}

class laptop extends computer {
public $ Display = "15 inches";

start public function () {
return "Laptop:". Parent:: Start ();
}
}

Here is the call:

$ My laptop → start ();

Now (at least in the book) the following issue should appear:

"Laptop: Computer starts."

The output is but: "Computer starts.".

Why is that so?

Ap

The parent function echo'd, the laptop class returned in the function only one string but does nothing. Replace return with echo, or write the return in ne variable and then echo the

ki

That can't work that way. In Laptop:: start is

return "Laptop:". Parent:: Start ();

The return is wrong at the point and must be replaced by an echo.

echo "Laptop:". Parent:: Start ();

Return returns values, but does not cause output on the screen. But since the only purpose of your function is to output text you have to use echo, otherwise nothing will happen.

Em

Quite simply, starting the function in the computer class will give an echo, whereas the class laptop will give you the value "laptop:" and the output of the function start the computer class (by the way, it is NULL / undefined, there's no return). It would be more useful if the function start in the computer class instead of echo using a return and you customize your function call as follows - by the way m.M.n. Also better style.

echo $ myLaptop → start ();

Ar

Strangely enough, the code in the book (which I downloaded and tested myself) works in the browser and outputs it correctly. Also with the return.

Ar

When I do this, "laptop" and "computer startup" will be output, but in a normal order. In line 1. Is there: "Computer starts" and in the bottom: "Laptop:". Real komich ^^

Ap

Well you know now what it was, the rest can certainly invent yourself

Ar

Yikes, it worked. Had the noble author also done so. I probably overlooked. But why does return work, not in this case? In Öaütpü class I can also echo'en, if I do not echo'e call.

Ar

Yes, I've got it, but why was it upside down and in different lines? No idea.

Ap

I do not know now exactly what you did, but that tells you already, that he is the parent function with the echo
has executed and then laptop: geechoed.

In principle, you should also returnen the "result" of the prtent function and then at the end when everything has gone through echo'en, so you avoid such confusions ^^

Le

No, it would only be good style if the method start would be called otherwise. However, the current name does not convey the information that something is being returned (as the name starteUndGibFeedback would do, for example).

So to keep the program intuitive, both methods should echo.

Em

Echo and return are 2 completely different things. I do not quite exaggerate the question.