Fatal error while extending an included class

I'm trying very simple here but it doesn't seem to work. Take a look at this code:

include 'custom/mainclass.php';

$child = new childClass();

class childClass extends mainClass {
}

      

Apparently childClass () cannot be found (according to php). I am 100% sure that I am doing something very stupid in order to streamline my code.

I have already searched the internet, but from what I understand, I am not doing anything wrong.

+2


source to share


2 answers


You must declare your classes in your code first before using them.

include 'custom/mainclass.php';

class childClass extends mainClass {

}

$child = new childClass(); //Create an instance after the class has been declared

      

EDIT:



After some research, it turned out that you can actually use a class before declaring it. But the declaration of the class and all parent classes must be in one file.

So, if you declare a parent class in one file and a child class in another, it won't work.

Also, you must first declare parent classes. After that, you can expand them.

+11


source


Same as with variables, functions or other language constructs,



Declare first, then use.

0


source







All Articles