multiple files using the same include file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will

    multiple files using the same include file

    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($fileNa me) {
    $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();

    $fileUsedInFirs t = $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
  • Oli Filth

    #2
    Re: multiple files using the same include file


    Will wrote:[color=blue]
    > I know I am searching using the wrong words because I can't seem to[/color]
    find[color=blue]
    > a simple answer on this. Here's what I want to do.
    >
    > helper.inc
    >
    > <!php[/color]
    ^
    That should be a "?"
    [color=blue]
    > var $fileName2;[/color]
    ^
    The member variable declaration should be inside the class.
    [color=blue]
    > class helper {
    >
    > function funcOne($fileNa me) {
    > $fp = fopen($fileName ,"r");
    > $fileName2 = $fileName;
    > **do stuff with the file**
    >
    > }
    >
    > function getFileName() {
    > return {$fileName2);[/color]
    ^
    You want rounded bracket here, not a curly brace.
    [color=blue]
    > }
    > }
    > ?>
    >
    > 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();
    >
    > $fileUsedInFirs t = $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[/color]
    to[color=blue]
    > 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[/color]
    this[color=blue]
    > 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[/color]
    [color=blue]
    > being reset. And there is the whole scope thing but is there a way[/color]
    to[color=blue]
    > preserve it for later use? It doesn't need to look anything like[/color]
    what I[color=blue]
    > have laid out.[/color]

    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

    Comment

    • Cylindric

      #3
      Re: multiple files using the same include file

      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

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

      Comment

      • Will

        #4
        Re: multiple files using the same include file

        Oli Filth wrote:[color=blue]
        > Will wrote:
        >[color=green]
        >>I know I am searching using the wrong words because I can't seem to[/color]
        >
        > find
        >[color=green]
        >>a simple answer on this. Here's what I want to do.
        >>
        >>helper.inc
        >>
        >><!php[/color]
        >
        > ^
        > That should be a "?"
        >
        >[color=green]
        >> var $fileName2;[/color]
        >
        > ^
        > The member variable declaration should be inside the class.
        >
        >[color=green]
        >> class helper {
        >>
        >> function funcOne($fileNa me) {
        >> $fp = fopen($fileName ,"r");
        >> $fileName2 = $fileName;
        >> **do stuff with the file**
        >>
        >> }
        >>
        >> function getFileName() {
        >> return {$fileName2);[/color]
        >
        > ^
        > You want rounded bracket here, not a curly brace.
        >
        >[color=green]
        >> }
        >> }
        >>?>
        >>
        >>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();
        >>
        >> $fileUsedInFirs t = $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[/color]
        >
        > to
        >[color=green]
        >>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[/color]
        >
        > this
        >[color=green]
        >>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[/color]
        >
        >[color=green]
        >>being reset. And there is the whole scope thing but is there a way[/color]
        >
        > to
        >[color=green]
        >>preserve it for later use? It doesn't need to look anything like[/color]
        >
        > what I
        >[color=green]
        >>have laid out.[/color]
        >
        >
        > 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.
        >[/color]

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

        Thanks,
        Will

        Comment

        • Will

          #5
          Re: multiple files using the same include file

          Cylindric wrote:[color=blue]
          > 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
          >
          > $fileUsedInFirs t = $_SESSION['first_helper']->getFileName( );
          >[/color]

          Thanks, that is exactly what I needed to know.

          Will

          Comment

          Working...