Question

I was checking joomla 1.6 index.php and I found the following code at the last line

echo $app;

this prints the entire page contents.

I just printed out the contents in this object using print_r() and I got the following details

JSite Object
(
    [template:JSite:private] => stdClass Object
        (
            [id] => 6
            [home] => 1
            [template] => beez5
            [params] => JRegistry Object
                (
                    [data:protected] => stdClass Object
                        (
                            [wrapperSmall] => 53
                            [wrapperLarge] => 72
                            [logo] => images/sampledata/fruitshop/fruits.gif
                            [sitetitle] => Matuna Market 
                            [sitedescription] => Fruit Shop Sample Site
                            [navposition] => left
                            [html5] => 0
                        )

                )

        )

    [_language_filter:JSite:private] => 
    [_detect_browser:JSite:private] => 
    [_clientId:protected] => 0
    [_messageQueue:protected] => Array
        (
        )

    [_name:protected] => site
    [scope] => 
    [requestTime] => 2011-10-17 17:23
    [startTime] => 1318872200.5365
    [_errors:protected] => Array
        (
        )

)

so how echo $app display all the site contents, it doesn't contains any HTML contents in the object.

Thank you very much

Was it helpful?

Solution

It declares the magic method __toString() in the class.

If this function is declared in a class, the return value of it will be used when the object is casted to a string.

Simple example: http://codepad.org/UmZUQA3v

OTHER TIPS

$app is an object, and print_r accesses its values in different ways from echo. When echo is called, it also implicitly calls the magic __toString method. That has been defined such that it returns a string with the page contents, given the values stored inside of the object. print_r will give you those values, but not the __toString representation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top