[Solved] Declaring objects and printing strings in PHP [closed]


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!

solved Declaring objects and printing strings in PHP [closed]