Error when extending a class

In one of the tutorials on youtube, he actually called the parent class from the index.html page but can I call it in my child page? For example: I have the following codes:

<?php

class NewClass extends parent {
	// properties and methods goes here....
    

}

$object = new NewClass;

echo $object->name;

and I have these codes in my parent class:


<?php


class parent {
	public $name = 'Hello there!';
}

But I got the following errors:

Fatal error: Cannot use ‘parent’ as class name as it is reserved in C:\xampp\htdocs\mmtuts\includes\newclass.inc.php on line 3

Again, you are jumping into this too fast. The thing I like about PHP is that its error messages are in plain English where anyone even a foreigner can understand it.

The error message means just that. You are trying to use the class parent which is a reserved name. Use something else.

2 Likes

You’ll find a lot of tutorials that use “placeholder” words where the author has assumed others will realize the name used is meant to indicate what the name represents. In this case “parent” means “the name of the parent class goes here” and not that the example will work as is if it is copy-pasted and run.

3 Likes

What editor do you used ?
try to use sublime-linter and php-lint in order to know your error while you code :slight_smile: … this helps me a lot :smiley:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.