Home>

I am studying PHP on the web. The last line is not displayed. What is wrong?
Expected display: hi, i am Bob!
Actual display: Fatal error ....

What should I do?
I don't know how to do this.
I use winndows10, Notepad ++, Xammp.
Please.

Notice: Undefined variable: bob in C: \ xampp \ htdocs \ php_sample \ php_lessons \ index_16.php on line 29
Fatal error: Uncaught Error: Call to a member function sayHi () on null in C: \ xampp \ htdocs \ php_sample \ php_lessons \ index_16.php: 29
Stack trace:
# 0 {main}
  thrown in C: \ xampp \ htdocs \ php_sample \ php_lessons \ index_16.php on line 29
<? php
    // Complex data structure
    // User class
    class User {
        // Property = variable of class
        // Define $name variable in Property
        public $name;

        // Constructor = called when a class is instantiated
        public function __construct ($name) {
            $this->name = $name;
        }
        // method
        public function sayHi () {
            echo "hi!, i am $this->name!";
        }
    }
    $tom = new User ("Tom");
    $bob = new User ("Bob");
    echo $tom->name;// Tom
    echo ('<pre>');// Line feed
    $bob->sayHi ();// hi, i am Bob!
?>
php
  • Answer # 1

    // ↓ Double-byte space is included
        $bob = new User ("Bob");

  • Answer # 2

    name = $name;
            }
            // method
            public function sayHi () {
                echo "hi!, i am {$this->name}!";
            }
        }
        $tom = new User ("Tom");
        $bob = new User ("Bob");
        echo $tom->name;// Tom
        echo ('
    ');// Line feed
        $bob->sayHi ();// hi, i am Bob!
    ?>

    A double-byte space ...
    I took it.

Related articles