p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: using the require function


Message #1 by "John " <jnapoleone@h...> on Tue, 8 Oct 2002 22:30:02
I am trying to include a file in several documents using the require 
function. These documents are located in several different directories all 
trying to "require" the same file. Furthermore, I understood that the 
require function is independent of the include_path setting of the 
php.ini. Yet when I:

use the syntax:

 require($DOCUMENT_ROOT . "/members/inc/copyright.inc");

I get the Error:

 Fatal error: Failed opening required '/members/inc/copyright.inc'    
(include_path='.:/usr/local/lib/php')

The failed path to the required file is correct, yet why am I getting the 
error?

Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 8 Oct 2002 14:29:04 -0700
It doesn't look like $DOCUMENT_ROOT is set to anything -- if it was, then the
error message would show more of a path then '/members...'.

Message #3 by "David Scott-Bigsby" <DScott-Bigsby@P...> on Tue, 8 Oct 2002 14:42:30 -0700
>>  require($DOCUMENT_ROOT . "/members/inc/copyright.inc");

> It doesn't look like $DOCUMENT_ROOT is set to anything -- if
> it was, then the
> error message would show more of a path then '/members...'.

That may not be the issue -- require() may be processed at a time when 
variables are not parsed.

From the manual:

"The require() statement replaces itself with the specified file, much 
like the C preprocessor's #include works.
....
require() is not actually a function in PHP; rather, it is a language 
construct. It is subject to some different rules than functions are. For 
instance, require() is not subject to any containing control structures. 
For another, it does not return any value; attempting to read a return 
value from a require() call results in a parse error."

I haven't tested my hunch, but I don't think require() will take 
anything other than a literal.

dsb

***************************************       
David Scott-Bigsby
Product Manager, Web Site and PEDN

PureEdge Solutions
The Leader in Secure XML e-Forms

v:250-708-8145  f:250-708-8010
1-888-517-2675   www.PureEdge.com
***************************************
Message #4 by "jorge" <jorge@d...> on Tue, 8 Oct 2002 17:07:39 -0500
hi john here is an example of how to use requiere and $ DOCUMENT_ROOT

require("$DOCUMENT_ROOT/canitec_actualizaciones/libjc_publica/conectar.php")
;

in your script i can see a dot "." just after DOCUMENT_ROOT pluss some " "

test your path as in my example

regards

Jorge Cordero

----- Original Message -----
From: "John " <jnapoleone@h...>
To: "beginning php" <beginning_php@p...>
Sent: Tuesday, October 08, 2002 10:30 PM
Subject: [beginning_php] using the require function


> I am trying to include a file in several documents using the require
> function. These documents are located in several different directories all
> trying to "require" the same file. Furthermore, I understood that the
> require function is independent of the include_path setting of the
> php.ini. Yet when I:
>
> use the syntax:
>
>  require($DOCUMENT_ROOT . "/members/inc/copyright.inc");
>
> I get the Error:
>
>  Fatal error: Failed opening required '/members/inc/copyright.inc'
> (include_path='.:/usr/local/lib/php')
>
> The failed path to the required file is correct, yet why am I getting the
> error?
>


Message #5 by "Nikolai Devereaux" <yomama@u...> on Tue, 8 Oct 2002 14:57:07 -0700
> hi john here is an example of how to use requiere and $ DOCUMENT_ROOT
>
> require("$DOCUMENT_ROOT/canitec../libjc../conectar.php");
>
> in your script i can see a dot "." just after DOCUMENT_ROOT pluss some " "


Jorge,

The . in John's code is the concatenation operator for strings.  Are you not
familiar with it?  It's one of the most basic operations that you have
available in PHP.

"hello" . ", " . "world"

results in the string "hello, world".

There is no difference between the result of a concatenation and variable
insertion.

in other words,

("$foo bar") == ($foo . " bar") == ("{$foo} bar")


nik

Message #6 by "Nikolai Devereaux" <yomama@u...> on Tue, 8 Oct 2002 15:26:55 -0700
You're wrong.  =)

I use require() all the time, and rarely do I use it without having some
variable in it.

Here's a cool trick that I've used before:


------ test_main.php ------
<?php

$classes = array('Stack', 'Queue', 'Deque', 'All');

if(isset($_POST['testswitch']))
{
   $test_selected = true;
   if($_POST['testswitch']['class'] == 'All')
   {
      foreach(array_diff($classes , array('All')) as $class)
      {
         echo "Testing $class:<br/>\n";
         require_once($class . '_driver.php');
         echo "<hr />\n";
      }
   }
   else
   {
      echo "Testing {$_POST['testswitch']['class']}:<br/>\n";
      require_once($_POST['testswitch']['class'] . '_driver.php');
      echo "<hr />\n";
   }
}

if($test_selected)
{
   echo "Run another?<br>"
}
else
{
   echo "Choose a class to test:<br>"
}

echo "<FORM method=\"POST\" action=\"{$_SERVER['PHP_SELF']}>\n";
foreach($classes as $class)
{
   echo "  <INPUT type=\"submit\" "
               . "name=\"testswitch[class]\" "
               . "value=\"{$class}\" />\n";
}
echo "</FORM>\n";

?>


In case it's not clear, what this does is let you pick a class object to test.
Assuming you follow a simple file convention, namely, each class has a test
driver written for it called "<classname>_driver.php", you can use this switch
statement to require one or more of these driver files.

Since require() basically inserts the contents of the included file in-place,
it's as if each required file executes as if you requested it directly.


Take care,

Nik

Message #7 by "John " <jnapoleone@h...> on Wed, 9 Oct 2002 02:26:52
> 
It doesn't look like $DOCUMENT_ROOT is set to anything -- if it was, then 
the
error message would show more of a path then '/members...'.

Thanks Nik and everyone for your input.

It turns out I neglected to recognize that the $DOCUMENT_ROOT was not 
evaluating to reflect the entire path to the file in question.

I spoke to the sys-admin and they have rectified things for me.

thanks
Message #8 by "David Scott-Bigsby" <DScott-Bigsby@P...> on Wed, 9 Oct 2002 10:01:04 -0700
> You're wrong.  =3D)
>
> I use require() all the time, and rarely do I use it without
> having some variable in it.

My conjecture was wrong -- oh well.

BTW, your example (below) doesn't use require(), but require_once().

According to the manual, require() should not work in this fashion

"require() is not actually a function in PHP; rather, it is a language 
construct. It is subject to some different rules than functions are. For 
instance, require() is not subject to any containing control structures. 
For another, it does not return any value; attempting to read a return 
value from a require() call results in a parse error.

Unlike include(), require() will always read in the target file, even if 
the line it's on never executes. If you want to conditionally include a 
file, use include(). The conditional statement won't affect the 
require(). However, if the line on which the require() occurs is not 
executed, neither will any of the code in the target file be executed.

Similarly, looping structures do not affect the behaviour of require(). 
Although the code contained in the target file is still subject to the 
loop, the require() itself happens only once."

Now, I don't conditionally include/require library files, so I'm not 
about to test this myself.

However I do wonder, if, as the manual says, that the code replaces the 
require() statement, how exactly your code below works without error? 
Doesn't your require_once() get replaced by the imported file? Are you 
taking advantage of a quirk in PHP that may not exist in future 
versions?

dsb

***************************************       
David Scott-Bigsby
Product Manager, Web Site and PEDN

PureEdge Solutions
The Leader in Secure XML e-Forms

v:250-708-8145  f:250-708-8010
1-888-517-2675   www.PureEdge.com
***************************************


> -----Original Message-----
> From: Nikolai Devereaux [mailto:yomama@u...]
> Sent: Tuesday, October 08, 2002 3:27 PM
> To: beginning php
> Subject: [beginning_php] RE: using the require function
>
>
>
> You're wrong.  =3D)
>
> I use require() all the time, and rarely do I use it without
> having some
> variable in it.
>
> Here's a cool trick that I've used before:
>
>
> ------ test_main.php ------
> <?php
>
> $classes =3D array('Stack', 'Queue', 'Deque', 'All');
>
> if(isset($_POST['testswitch']))
> {
>    $test_selected =3D true;
>    if($_POST['testswitch']['class'] =3D=3D 'All')
>    {
>       foreach(array_diff($classes , array('All')) as $class)
>       {
>          echo "Testing $class:<br/>\n";
>          require_once($class . '_driver.php');
>          echo "<hr />\n";
>       }
>    }
>    else
>    {
>       echo "Testing {$_POST['testswitch']['class']}:<br/>\n";
>       require_once($_POST['testswitch']['class'] . '_driver.php');
>       echo "<hr />\n";
>    }
> }
>
> if($test_selected)
> {
>    echo "Run another?<br>"
> }
> else
> {
>    echo "Choose a class to test:<br>"
> }
>
> echo "<FORM method=3D\"POST\" action=3D\"{$_SERVER['PHP_SELF']}>\n";
> foreach($classes as $class)
> {
>    echo "  <INPUT type=3D\"submit\" "
>                . "name=3D\"testswitch[class]\" "
>                . "value=3D\"{$class}\" />\n";
> }
> echo "</FORM>\n";
>
> ?>
>
>
> In case it's not clear, what this does is let you pick a
> class object to test.
> Assuming you follow a simple file convention, namely, each
> class has a test
> driver written for it called "<classname>_driver.php", you
> can use this switch
> statement to require one or more of these driver files.
>
> Since require() basically inserts the contents of the
> included file in-place,
> it's as if each required file executes as if you requested it
> directly.
>
>
> Take care,
>
> Nik
>
>
>
Message #9 by "Nikolai Devereaux" <yomama@u...> on Thu, 10 Oct 2002 14:24:26 -0700
> BTW, your example (below) doesn't use require(), but require_once().

yeah, but: "This is a behavior similar to the require() statement, with the
only difference being that if the code from a file has already been included,
it will not be included again."

> According to the manual, require() should not work in this fashion

Where does the manual say that this won't work?

Check out the second line in the first example code box:

require $somefile;


Looks like require can accept variables as input params to me!


> Unlike include(), require() will always read in the target file,
> even if the line it's on never executes. If you want to
> conditionally include a file, use include(). The conditional
> statement won't affect the require(). However, if the line on which
> the require() occurs is not executed, neither will any of the code
> in the target file be executed.

Ahhh, okay.  Now it's starting to make sense, I think.  This paragraph appears
in this note, but there is a very important note preceeding it:

"Note: Prior to PHP 4.0.2, the following applies:"

So that means you're getting your information from a very outdated manual.

Take care,

Nik



  Return to Index