By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,740 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,740 IT Pros & Developers. It's quick & easy.

multiple files using the same include file

P: n/a
I know I am searching using the wrong words because I can't seem to find
a simple answer on this. Here's what I want to do.

helper.inc

<!php
var $fileName2;
class helper {

function funcOne($fileName) {
$fp = fopen($fileName,"r");
$fileName2 = $fileName;
**do stuff with the file**

}

function getFileName() {
return {$fileName2);
}
}
?>

first.php
<html>
<?php
require('helper.inc');
$helper_first = new helper();

$helper_first->funcOne("info.txt");

*****
Lay out page based on info.txt
including link to second.php
*****
?>
</html

second.php
<html>
<?
require('helper.inc');
$helper_second = new helper();

$fileUsedInFirst = $helper_second->getFileName();

*****
Lay out page based on info.txt
including link to second.php
*****
?>
</html>
Hope that makes some sense. Basically I will have many pages like
first.php that have the info file (info.txt) I want to use. I want to
be able to grab that information and use it in second.php. I know I
could pass it through the url but now that I have been trying to do this
I want to see what point(s) I am missing. I know the constructor (I
didn't include this in the example)is called each time so the var are
being reset. And there is the whole scope thing but is there a way to
preserve it for later use? It doesn't need to look anything like what I
have laid out.

Thanks,
Will
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Will wrote:
I know I am searching using the wrong words because I can't seem to find a simple answer on this. Here's what I want to do.

helper.inc

<!php ^
That should be a "?"
var $fileName2; ^
The member variable declaration should be inside the class.
class helper {

function funcOne($fileName) {
$fp = fopen($fileName,"r");
$fileName2 = $fileName;
**do stuff with the file**

}

function getFileName() {
return {$fileName2); ^
You want rounded bracket here, not a curly brace.
}
}
?>

first.php
<html>
<?php
require('helper.inc');
$helper_first = new helper();

$helper_first->funcOne("info.txt");

*****
Lay out page based on info.txt
including link to second.php
*****
?>
</html

second.php
<html>
<?
require('helper.inc');
$helper_second = new helper();

$fileUsedInFirst = $helper_second->getFileName();

*****
Lay out page based on info.txt
including link to second.php
*****
?>
</html>
Hope that makes some sense. Basically I will have many pages like
first.php that have the info file (info.txt) I want to use. I want to be able to grab that information and use it in second.php. I know I
could pass it through the url but now that I have been trying to do this I want to see what point(s) I am missing. I know the constructor (I
didn't include this in the example)is called each time so the var are being reset. And there is the whole scope thing but is there a way to preserve it for later use? It doesn't need to look anything like what I have laid out.


I don't understand what it is that you're trying to do.

When you run second.php, a completely new instance of the helper class
is produced, which has no reference to the original one.

If you want to share variables between successive scripts (i.e.
first.php and second.php), either write to a file, a database, put it
in the URL, or use a session variable.

--
Oli

Jul 17 '05 #2

P: n/a
I don't think the variables and classes defined in first.php will be
visible in second.php unless you store them in a session or cookie or
database or something.

The bottom of first.php might need to have a

$_SESSION['first_helper'] = $helper_first;

and then the second.php could contain

$fileUsedInFirst = $_SESSION['first_helper']->getFileName();

Jul 17 '05 #3

P: n/a
Oli Filth wrote:
Will wrote:
I know I am searching using the wrong words because I can't seem to


find
a simple answer on this. Here's what I want to do.

helper.inc

<!php


^
That should be a "?"

var $fileName2;


^
The member variable declaration should be inside the class.

class helper {

function funcOne($fileName) {
$fp = fopen($fileName,"r");
$fileName2 = $fileName;
**do stuff with the file**

}

function getFileName() {
return {$fileName2);


^
You want rounded bracket here, not a curly brace.

}
}
?>

first.php
<html>
<?php
require('helper.inc');
$helper_first = new helper();

$helper_first->funcOne("info.txt");

*****
Lay out page based on info.txt
including link to second.php
*****
?>
</html

second.php
<html>
<?
require('helper.inc');
$helper_second = new helper();

$fileUsedInFirst = $helper_second->getFileName();

*****
Lay out page based on info.txt
including link to second.php
*****
?>
</html>
Hope that makes some sense. Basically I will have many pages like
first.php that have the info file (info.txt) I want to use. I want


to
be able to grab that information and use it in second.php. I know I
could pass it through the url but now that I have been trying to do


this
I want to see what point(s) I am missing. I know the constructor (I
didn't include this in the example)is called each time so the var are


being reset. And there is the whole scope thing but is there a way


to
preserve it for later use? It doesn't need to look anything like


what I
have laid out.

I don't understand what it is that you're trying to do.

When you run second.php, a completely new instance of the helper class
is produced, which has no reference to the original one.

If you want to share variables between successive scripts (i.e.
first.php and second.php), either write to a file, a database, put it
in the URL, or use a session variable.


Sorry for all the bone head typos. You told me what I needed to know.
I think I will use session variables.

Thanks,
Will
Jul 17 '05 #4

P: n/a
Cylindric wrote:
I don't think the variables and classes defined in first.php will be
visible in second.php unless you store them in a session or cookie or
database or something.

The bottom of first.php might need to have a

$_SESSION['first_helper'] = $helper_first;

and then the second.php could contain

$fileUsedInFirst = $_SESSION['first_helper']->getFileName();


Thanks, that is exactly what I needed to know.

Will
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.