views:

36

answers:

3

If I have:

<?php
require_once("bla.php");
class controller{.....}
php>

If I then create in a different file class control_A extends controller{...}, do I need to again say require_once("bla.php");, or is it inherited?

What if the require_once is done inside the class controller definition?

Thanks!

JDelage

A: 

Yes, you do. require_once "foo.php" effectively takes the contents of foo.php and pastes them in (and then processes all of its requires and includes).

Christian Mann
Your wording doesn't make sense. You say that it processes the included files requires/includes but then you also say that you need to require the file separately (which you don't).
Blair McMillan
Ah, I see the issue here. Say he defines `class Foo` in `foo.php`, and `includes` some library in that file. By virtue of extending the class, he won't automatically include that library. However, because he is including `foo.php`, he does include the library in question. Does that make sense?
Christian Mann
+2  A: 

As long as you're including the inherited class, it doesn't need requiring again.

I.e.

class1.php:

<?php
require_once("tools.php");
class class1 {

}
?>

class2.php:

<?php
require_once("class1.php");
class class2 extends class1 {

}
?>
Mikee
+2  A: 

So far we have two contradicting, but equally correct answers =) Let's see if I can't combine the two into a more specific total answer.

If any class requires any code or definitions in bla.php, then you will need to include("bla.php") at least one time in the entire run-time of your script. If your previous code:

<?php
require_once("bla.php");
class controller{.....}
php>

is in the file controller.php then you can create control_A in the following way:

<?php
require_once("controller.php");
class control_A extends controller{...}
?>

This is because the require_once() function essentially copies and pastes the contents of the file into the script at that line. Therefore the above will be seen equivalent to this:

<?php
/* INSERTED FROM controller.php */
/* INSERTED FROM bla.php */
necessary definitions for controller
/* END bla.php */
class controller{.....}
/* END controller.php */
class control_A extends controller{...}
?>

As you can see, just by requiring controller.php, the necessary definitions for controller are now seen and parsed. What you can not do is omit the declaration of controller. This isn't just because you required bla.php while declaring it, but also you can't extend a class which hasn't yet been declared. So the following code:

<?php
class control_A extends controller{...}
?>

will give you an error since controller hasn't been defined.

One thing to consider, however- since the class controller doesn't extend any other class, it should not have any external dependencies. There's a good chance that whatever you do in bla.php which must run before defining the class is either unnecessary or can be restructured. What exactly is bla.php doing that you need before defining controller?

steven_desu