dougong1031 2015-02-15 01:54
浏览 22
已采纳

在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();
?>
  • 写回答

1条回答 默认 最新

  • duanjiancong4860 2015-02-15 04:31
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计