Results 1 to 2 of 2

Thread: echo method + static method

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default echo method + static method

    i am trying to echo some strings like this
    Code:
    $about = new Model_About();
    echo $about->beforeHeader() . ' + ' . Model_About::sFunc();
    but it outputs this: string before headerstatic string +
    why does this output in the end?
    Code:
    . ' + ' .
    class
    Code:
    class Model_About
    {
    	private $str = 'string before header';
    	private static $statStr = 'static string';
    	
    	function beforeHeader()
    	{
    		echo $this->str;
    	}
    	static function sFunc()
    	{
    		echo self::$statStr;
    	}
    }
    Last edited by ggalan; 05-29-2011 at 12:58 PM.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    because of the order you're doing things in. when this
    PHP Code:
    echo $about->beforeHeader() . ' + ' Model_About::sFunc();
    // I'll refer to this line as "the call" in my explanation 
    is executed, things happen in this order:

    1) beforeHeader() runs. it echos $str to the browser. it has no return value, so we end up with this:
    --browser output: "string before header"
    --"the call" value: ""

    2) once the function is done, the spaces and plus sign are concatenated to the value that the call will echo (in fact, as you'll see, this is the only value that "the call" will echo):
    --browser output: "string before header"
    --"the call" value: " + "

    3) sFunc() runs. it echos $statStr, but like the first function, has no return value:
    --browser output: "string before headerstatic string"
    --"the call" value: " + "

    4) now we're at the end of the statement, so the value of "the call" is echoed:
    --browser output: "string before headerstatic string + "

    And that is that.

    When you start using functions, classes, etc., in more complex ways, it is far more useful to return values rather than echoing them. Once you have a complete result from your script, echo it all at once.
    PHP Code:
    $about = new Model_About();

    echo 
    $about->beforeHeader() . ' + ' Model_About::sFunc();
    // outputs:  "string before header + static string"

    // you could also do this (same result):
    // $printthis = $about->beforeHeader() . ' + ' . Model_About::sFunc();
    // echo $printthis;

    class Model_About
    {
        private 
    $str 'string before header';
        private static 
    $statStr 'static string';
        
        function 
    beforeHeader()
        {
            return 
    $this->str;
    //        note: "return", NOT "echo"!
        
    }
        static function 
    sFunc()
        {
            return 
    self::$statStr;
        }

    Last edited by traq; 05-29-2011 at 07:38 AM. Reason: clarification

  3. The Following User Says Thank You to traq For This Useful Post:

    ggalan (05-29-2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •