在PHP中声明对象和打印字符串[关闭]

Catchable fatal error: Object of class HelloWorldClass could not be converted to string in /Applications/XAMPP/xamppfiles/htdocs/wisd_activity04c.php on line 19

This program is supposed to output 1. "Hello World!" in red/40px font 2. "Hello World!" in green/20px font and underlined

<?php 
    echo '60-334 ACTIVITY 4 PART 3/3<br><br>';

    class HelloWorldClass
    { 
        public $font_size;
        public $font_colour;
        public $hello_string;

        function __construct($size, $colour)
        { 
            $this->font_size = $size;
            $this->font_colour = $colour;
            $this->hello_string = "Hello World!";
        } 

        public function custom_show() 
        { 
            echo "<font color=\"$this.font_colour\" size=\"$this.font_size\">$this.hello_string</font>";
        }  
    } 

    class Sub_HelloWorldClass extends HelloWorldClass 
    { 
        function __construct($size, $colour)
        {
            parent::__contruct($font, $colour);
        }

        public function custom_show() 
        { 
            echo "<u><font color=\"$this.font_colour\" size=\"$this.font_size\">$this.hello_string</font></u>";
        } 
    } 

    $object = new HelloWorldClass('40px', 'red');
    $object->custom_show();

    $object = new Sub_HelloWorldClass('20px', 'green');
    $object->custom_show();
?>

This answer is from your first revision:

https://stackoverflow.com/revisions/28522294/1

You have a few errors in your code:

1. Missing semicolon

$this->hello_string = "Hello World!"  //<- Missing semicolon at the end

2. Wrong access of class property's

echo "<font color=\"$this.font_colour\" size=\"$this.font_size\">$this.hello_string</font>";
//...
echo "<u><font color=\"$this.font_colour\" size=\"$this.font_size\">$this.hello_string</font></u>";

I recommend you to concatenate the property's with the string. How to concatenate? You have to use the concatenation operator: . and also determ the string with the quotes.

Additional to that you also have to access a class property with the operator: ->. For more information about accessing class property's see the manual: http://php.net/manual/en/language.oop5.properties.php

So your code should look something like this:

echo "<font color=\"" . $this->font_colour . "\" size=\"" . $this->font_size . "\">" . $this->hello_string . "</font>";
//...                 ^ See here concatenation                   ^^ See here access of class property
echo "<u><font color=\"" . $this->font_colour . "\" size=\"" . $this->font_size . "\">" . $this->hello_string . "</font></u>";

3. Class name with space

You can't have a class name with spaces:

class Sub_ HelloWorldClass extends HelloWorldClass  //So change just remove the space

For more information about class names see the manual: http://php.net/manual/en/language.oop5.basic.php

And a quote from there:

The class name can be any valid label, provided it is not a PHP reserved word. A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$.

4. Missing 's' in __construct()

parent::__contruct($font, $colour);  //Missed 's' in construct

5. Wrong variable used

function __construct($size, $colour)
{
    parent::__construct($font, $colour);  //Change '$font' to '$size'
}

Side Note:

Turn on error reporting at the top of your file(s) only while staging:

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>

This will provide you useful error messages which show's you very well where the error is!