5

I read a post here that the person wrote a statement like :

 $this->_connection = require_once 'config.php';

   // $this->connection is an array variable.

I find it a little bit hard to understand. Am asking myself how can you assign an included file to a variable.

Does it mean that an array must be returned from the "config.php" file? I mean should "config.php" return an array?

Is such statement good in commercial php applications?

Thank you.

Community
  • 1
  • 1
Marko Morris
  • 141
  • 1
  • 6
  • Anything goes in "commercial" PHP applications. What you really want to know is whether it's good in a "well-designed" PHP application. (In which case I would avoid it, though it's not necessarily bad.) – Waleed Khan Dec 29 '12 at 21:56
  • Yes, a value must be returned in config.php - it's actually very handy occasionally. I suspect you'd need to wrap the pathname in brackets though (or, at least, it appears clearer if you do). I seem to recall that Propel uses this approach. – halfer Dec 29 '12 at 21:57
  • I WANT TO THANK ALL OF YOU GUYS FOR YOUR ANSWERS – Marko Morris Dec 29 '12 at 22:03

1 Answers1

7

The included file may have a return statement outside of any function. If this happens, the script stops running the included file and the "return value" of the require_once call is the value of the return statement.

Docs

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592