0

I was writing a simple class. Here is the code:

class Book{
    var $title;
    var $publishedDate;

    function Book($title, $publishedDate){
        $this->title = $title;
        $this->publishedDate = $publishedDate;
    }

    function displayBook(){
        echo "Title: " . $this->title . " published on " . $this->publishedDate . "";
    }

    function setAndDisplay($newTitle, $newDate){
        $this->title = $newTitle;
        $this->publishedDate = $newDate;
        echo "The new information is of the book <b>" . $this->displayBook() . "</b><br />";
    }
}

And I initialized the class and called the function:

$test = new Book("Harry Potter", "2010-1-10");
$test->setAndDisplay("PHP For Beginners", "2010-2-10");

And the result is:

"Title: PHP For Beginners published on 2010-2-10The new information is of the book"

Shouldn't it be:

"The new information is of the book **Title: PHP For Beginners published on 2010-2-10**

Can anyone explain?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
progamer
  • 69
  • 6

2 Answers2

3

The displayBook() method does not return a string (or anything for that matter) so you shouldn't really be using the result in a concatenation.

What's happening is the call to displayBook() in the echo in setAndDisplay() happens before the echo completes.

You should use different methods for direct output vs string generation, eg

public function getBook()
{
    return sprintf('Title: %s published on %s',
                   $this->title,
                   $this->publishedDate);
}

public function displayBook()
{
    echo $this->getBook();
}

public function setAndDisplay($newTitle, $newDate)
{
    $this->title = $newTitle;
    $this->publishedDate = $newDate;
    echo "The new information is of the book <b>", $this->getBook(), "</b><br />";
}

Edit: I'd seriously re-evaluate the need for your class to directly echo data. This is rarely a good idea.

Edit2: Passing parameters to echo is faster than concatenation

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Hmmm... I see. So I must store in a variable first, before do the concatination? – progamer Nov 10 '10 at 03:07
  • @wordpress_newbie - Sorry, I'm a slow typist today. See my edit – Phil Nov 10 '10 at 03:11
  • Learned another tips => "Passing parameters to echo is faster than concatenation". All the while, I used concatenation("."). – progamer Nov 10 '10 at 03:21
  • 1 quick question: Is there any limit of how many arguments you can pass to "echo" function? Sorry, its out of this topic, but I guess, this is a quick/short question, so I dont want to open up a new question. – progamer Nov 10 '10 at 03:23
  • 1
    The best place for this kind if information is the manual - http://www.php.net/manual/en/function.echo.php. It's not mentioned so I'd say there's no limit within *reasonable* bounds. – Phil Nov 10 '10 at 03:33
1

It's because displayBook() echos the string. And when you try to append or inject a function that echos something, it will get put at the beginning. You have to replace the dots . with commas ,for it to get placed where you want it.

http://codepad.org/9pfqiRHu

Baylor Rae'
  • 3,995
  • 20
  • 39