Mohd Shahril
Mohd Shahril

Reputation: 2327

PHP extending other class and vice versa

Sorry for the bad title, i don't have any idea what title i should put on top.

Ok, here my question, this code maybe can tell you what problem i have right now..

File : -

utilities.php -> class utilitites
http.php -> class http

In http.php file

<?php

required_once('utilities.php');

class http extends utilities {
// code right here..
}

?>

In utilities.php file

<?php

required_once('http.php');

class utilities extends http {
// code right here..
}

?>

I'm getting error in utilities.php file, it's said..

Fatal error: Class 'http' not found in C:\xampp\htdocs\project\utilities.php on line 5

So what problem is really do I have ?

Upvotes: 1

Views: 121

Answers (4)

Nicholas Gerrer
Nicholas Gerrer

Reputation: 43

When working with inheritance you generally want a child class to inherit a parent class' properties and methods. In your example and as pointed out by some of the other answers, you are making both the 'http' class and the 'utilities' class try and act as both parent and child to one another. You may want to rethink what exactly you are trying to accomplish with inheritance. Figure out which class should be the parent and what properties/methods the child class needs to use from the parent. What properties/methods might be similar but slightly different?

Upvotes: 1

Orangepill
Orangepill

Reputation: 24645

Class Hierarchies are exactly that heirarchies.

This means it is top down and acyclic.

This would be like you being your own grandfather and I don't even thing the worst Back To The Future Fan Fiction would go there.

Upvotes: 1

Anas Bouhtouch
Anas Bouhtouch

Reputation: 546

you're trying to extend a class that doesn't exist that is "http", you can't do what you're trying to do. Why you should extend a class that extends the first one, this is pretty non-sense.

Can't you have a 3rd class and both http and utilities extend this last one?

Upvotes: 1

Mark Reed
Mark Reed

Reputation: 95242

The problem is that utilities inherits from http which inherits from utilities which inherits from http which inherits from utilities which inherits from http which . . . there's no way to break the cycle. Chances are whatever problem you are trying to solve has a solution that doesn't require this mutual-inheritance setup.

Upvotes: 2

Related Questions