1

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 ?

5
  • Can you explain what the base reason you need to do this? We can likely suggest a different way to accomplish what you're trying to do, as you currently have a cyclical dependency that isn't likely to work at all. Commented May 29, 2013 at 2:31
  • It doesn't make any more sense for two classes to both be each other's child than for two people to both be each other's child. What problem are you trying to solve? Commented May 29, 2013 at 2:34
  • there are some function in http class that i must use in utilities class, i also required some function from utilities that required in http class.. Commented May 29, 2013 at 2:43
  • Then you need to reorganises the classes to resolve the circular dependency. Commented May 29, 2013 at 2:50
  • A function doesn't have to be defined inside a class for other functions in the class to call it. Each class could create an instance of the other and call methods on that instance, for example. That won't cause an infinitely recursive dependency. Commented May 29, 2013 at 3:37

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, i never think about the cycle until i read your answers.. I already repair my class and all have work correctly.. :)
1

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?

Comments

1

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.

Comments

1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.