Reputation: 2970
I am trying a very simple thing 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'm 100% sure I'm doing something very stupid in the way of ordering my code.
I already searched the web but from what I understand I'm doing nothing wrong..
Upvotes: 2
Views: 1704
Reputation: 7784
You have to 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, declaration of the class and all parent classes must be in the same file.
So if you declare a parent class in one file and a child class in another, it won't work.
Also, you must declare parent classes first. After that you can extend them.
Upvotes: 11
Reputation: 5856
Same as with Variables, Functions or other language constructs,
First declare, then use.
Upvotes: 1