How to keep global strings in another file?

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

    How to keep global strings in another file?

    Hi!

    I want to have a number of strings in another file, which I can
    include everywhere... but I cannot make them reachable from the top
    file. I have looked at var and global to to this but without luck.
    The idea is to have all text the same place, which minimses changes
    and spelling errors.

    something like error messages stored:

    <?
    $sError001="stu pid user";
    $sError002="not available";
    ?>

    and then :

    include "errormsgs.php" ;
    echo $sError001;

    BR
    Sonnich

  • Geoff Berrow

    #2
    Re: How to keep global strings in another file?

    Message-ID: <1173963662.556 376.252970@b75g 2000hsg.googleg roups.comfrom
    Sonnich contained the following:
    >something like error messages stored:
    >
    ><?
    >$sError001="st upid user";
    >$sError002="no t available";
    >?>
    >
    >and then :
    >
    >include "errormsgs.php" ;
    >echo $sError001;
    Well yeah, that will work, so what is the question?

    Though personally, I'd use an array.

    <?
    $sError[1]="stupid user";
    $sError[2]="not available";

    ?>
    >
    >and then :
    >
    include "errormsgs.php" ;
    echo $sError[1];
    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • Floortje

      #3
      Re: How to keep global strings in another file?

      Sonnich schreef:
      Hi!
      >
      I want to have a number of strings in another file, which I can
      include everywhere... but I cannot make them reachable from the top
      file. I have looked at var and global to to this but without luck.
      The idea is to have all text the same place, which minimses changes
      and spelling errors.
      >
      something like error messages stored:
      >
      <?
      $sError001="stu pid user";
      $sError002="not available";
      ?>
      >
      and then :
      >
      include "errormsgs.php" ;
      echo $sError001;
      This should work. You have another error. \

      Btw set it up a little different. That way you can allways change ur
      error code without redoing the same echo over and over again.

      include('somefi le');
      showerror(1);

      function showerror($no)
      {
      $errors=array(
      1=>'wrong page',
      2=>'hack attempt'
      )

      echo $errors[$no];
      // do stuff here later
      }

      --
      Arjen
      HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

      Comment

      • Sonnich

        #4
        Re: How to keep global strings in another file?

        On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog .co.ukwrote:
        Message-ID: <1173963662.556 376.252970@b75g 2000hsg.googleg roups.comfromSo nnichcontained the following:
        >
        something like error messages stored:
        >
        <?
        $sError001="stu pid user";
        $sError002="not available";
        ?>
        >
        and then :
        >
        include "errormsgs.php" ;
        echo $sError001;
        >
        Well yeah, that will work, so what is the question?
        The point it, that it does not work.

        Comment

        • Floortje

          #5
          Re: How to keep global strings in another file?

          Sonnich schreef:
          On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog .co.ukwrote:
          >Message-ID: <1173963662.556 376.252970@b75g 2000hsg.googleg roups.comfromSo nnichcontained the following:
          >>
          >>something like error messages stored:
          >><?
          >>$sError001="s tupid user";
          >>$sError002="n ot available";
          >>?>
          >>and then :
          >>include "errormsgs.php" ;
          >>echo $sError001;
          >Well yeah, that will work, so what is the question?
          >
          The point it, that it does not work.
          >
          And his point is that it does work :-) You have another error somewhere
          ... are you sure you have included the correct file?

          try this->
          errormsg.php

          echo "you have included errormsg.php";

          --
          Arjen
          HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

          Comment

          • Sonnich

            #6
            Re: How to keep global strings in another file?

            On Mar 15, 3:32 pm, Floortje <l...@zingmaarm etmijmee.enelwr ote:
            Sonnichschreef: On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog .co.ukwrote:
            Message-ID: <1173963662.556 376.252...@b75g 2000hsg.googleg roups.comfromSo nnichcontained the following:
            >
            >something like error messages stored:
            ><?
            >$sError001="st upid user";
            >$sError002="no t available";
            >?>
            >and then :
            >include "errormsgs.php" ;
            >echo $sError001;
            Well yeah, that will work, so what is the question?
            >
            The point it, that it does not work.
            >
            And his point is that it does work :-) You have another error somewhere
            .. are you sure you have included the correct file?
            I found, that my problem is to include this into a class...

            include...

            class whatever
            {
            $somthing= $somthing_from_ include_file.

            In other words, I want to export texts used in my class to a file...

            /S

            Comment

            • Floortje

              #7
              Re: How to keep global strings in another file?

              Classes are somewhat different. Run this code (maybe fix a minor speling
              error .. I didn't test) and compare it with your code.

              class a
              {
              public $error1

              public function a()
              {
              $this->error1='howdy' ;
              }
              }

              class b extends a
              {
              private function b()
              {
              echo $this->error1;
              }


              class c
              {
              private function c()
              {
              global $a
              echo $a->error1;
              }
              }

              $a=new a
              $b=new b
              $c=new c

              --
              Arjen
              HondenPage: alles over uw hond of honden,fokkers en puppy's. Je vindt hier het hondenforum, honden foto's, fokkers, puppy's, de honden encyclopedie en nog veel meer !

              Comment

              • Jerry Stuckle

                #8
                Re: How to keep global strings in another file?

                Sonnich wrote:
                On Mar 15, 3:32 pm, Floortje <l...@zingmaarm etmijmee.enelwr ote:
                >Sonnichschreef :On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog .co.ukwrote:
                >>>Message-ID: <1173963662.556 376.252...@b75g 2000hsg.googleg roups.comfromSo nnichcontained the following:
                >>>>something like error messages stored:
                >>>><?
                >>>>$sError001= "stupid user";
                >>>>$sError002= "not available";
                >>>>?>
                >>>>and then :
                >>>>include "errormsgs.php" ;
                >>>>echo $sError001;
                >>>Well yeah, that will work, so what is the question?
                >>The point it, that it does not work.
                >And his point is that it does work :-) You have another error somewhere
                >.. are you sure you have included the correct file?
                >
                I found, that my problem is to include this into a class...
                >
                include...
                >
                class whatever
                {
                $somthing= $somthing_from_ include_file.
                >
                In other words, I want to export texts used in my class to a file...
                >
                /S
                >
                It would help if you would post your entire code - not just little pieces.

                For instance, your first statement was:

                "I want to have a number of strings in another file, which I can include
                everywhere... but I cannot make them reachable from the top file.".

                Now you're saying:

                "In other words, I want to export texts used in my class to a file..."

                Two entirely different things.

                And BTW - you should be using <?php instead of just <?. Most hosts
                nowadays are running with short_tags_off.


                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • Sonnich

                  #9
                  Re: How to keep global strings in another file?

                  On Mar 15, 5:34 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                  Sonnichwrote:
                  On Mar 15, 3:32 pm, Floortje <l...@zingmaarm etmijmee.enelwr ote:
                  Sonnichschreef: On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog .co.ukwrote:
                  >>Message-ID: <1173963662.556 376.252...@b75g 2000hsg.googleg roups.comfromSo nnichcontained the following:
                  >>>something like error messages stored:
                  >>><?
                  >>>$sError001=" stupid user";
                  >>>$sError002=" not available";
                  >>>?>
                  >>>and then :
                  >>>include "errormsgs.php" ;
                  >>>echo $sError001;
                  >>Well yeah, that will work, so what is the question?
                  >The point it, that it does not work.
                  And his point is that it does work :-) You have another error somewhere
                  .. are you sure you have included the correct file?
                  >
                  I found, that my problem is to include this into a class...
                  >
                  include...
                  >
                  class whatever
                  {
                  $somthing= $somthing_from_ include_file.
                  >
                  In other words, I want to export texts used in my class to a file...
                  >
                  /S
                  >
                  It would help if you would post your entire code - not just little pieces.
                  >
                  For instance, your first statement was:
                  >
                  "I want to have a number of strings in another file, which I can include
                  everywhere... but I cannot make them reachable from the top file.".
                  >
                  Now you're saying:
                  >
                  "In other words, I want to export texts used in my class to a file..."
                  >
                  Two entirely different things.
                  Hardly. I could also export all error messages to a file. I can take
                  any text and put it in a file. Appearently we have different opnions
                  about this.

                  Code as I have it:

                  Include file:
                  <?php
                  $test="hello";

                  //---------------------------------- header for sheets...
                  $sSheet_001="bl ablaabla


                  File, which uses the include file:

                  <?php
                  include '/functions.php'; <------------ basic functions and other
                  stuff, used in class.
                  include 'cls_inc.php'; <----------- here I include the file

                  class clsMyClass
                  {
                  var $Contens = array();

                  function SaveFile($filen ame)
                  {
                  // extract values for later use
                  $name=ExtractFi lename($filenam e); <--------- functions from
                  functions.php
                  $path=ExtractFi lePath($filenam e);
                  $i=strrpos($nam e, ".");
                  if($i===false)
                  $files=$name."_ files";
                  else
                  $files=substr($ name, 0, $i)."_files";
                  $filesdir=$path ."\\".$files ;
                  // create directory for
                  if(!file_exists ($filesdir))
                  mkdir($filesdir );

                  $fFile=fopen($f ilesdir."\\page ".Format($i , 3).".htm", "w");
                  fwrite($fFile, $sSheet_001); <------------ this is where the
                  string is used...
                  // save contens of page
                  for($y=0;$y<cou nt($this->Contens[$i]);$y++)
                  {
                  fwrite($fFile, "<tr>...
                  .....

                  Does this explain?

                  BR
                  Sonnich

                  Comment

                  • Rik

                    #10
                    Re: How to keep global strings in another file?

                    Sonnich <sonnich.jensen @elektrobit.com wrote:
                    On Mar 15, 5:34 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                    >Sonnichwrote :
                    On Mar 15, 3:32 pm, Floortje <l...@zingmaarm etmijmee.enelwr ote:
                    >Sonnichschreef :On Mar 15, 3:20 pm, Geoff Berrow
                    ><blthe...@ckdo g.co.ukwrote:
                    >>>Message-ID:
                    ><1173963662.55 6376.252...@b75 g2000hsg.google groups.com
                    >fromSonnichcon tained the following:
                    >>>>something like error messages stored:
                    >>>><?
                    >>>>$sError001= "stupid user";
                    >>>>$sError002= "not available";
                    >>>>?>
                    >>>>and then :
                    >>>>include "errormsgs.php" ;
                    >>>>echo $sError001;
                    >>>Well yeah, that will work, so what is the question?
                    >>The point it, that it does not work.
                    >And his point is that it does work :-) You have another error
                    >somewhere
                    >.. are you sure you have included the correct file?
                    >>
                    I found, that my problem is to include this into a class...
                    >>
                    include...
                    >>
                    class whatever
                    {
                    $somthing= $somthing_from_ include_file.
                    >>
                    In other words, I want to export texts used in my class to a file....
                    >>
                    /S
                    >>
                    >It would help if you would post your entire code - not just little
                    >pieces.
                    >>
                    >For instance, your first statement was:
                    >>
                    >"I want to have a number of strings in another file, which I can include
                    >everywhere.. . but I cannot make them reachable from the top file.".
                    >>
                    >Now you're saying:
                    >>
                    >"In other words, I want to export texts used in my class to a file..."
                    >>
                    >Two entirely different things.
                    >
                    Hardly. I could also export all error messages to a file. I can take
                    any text and put it in a file. Appearently we have different opnions
                    about this.
                    >
                    Code as I have it:
                    >
                    Include file:
                    <?php
                    $test="hello";
                    >
                    //---------------------------------- header for sheets...
                    $sSheet_001="bl ablaabla
                    >
                    >
                    File, which uses the include file:
                    >
                    <?php
                    include '/functions.php'; <------------ basic functions and other
                    stuff, used in class.
                    include 'cls_inc.php'; <----------- here I include the file
                    >
                    class clsMyClass
                    {
                    var $Contens = array();
                    >
                    function SaveFile($filen ame)
                    {
                    // extract values for later use
                    $name=ExtractFi lename($filenam e); <--------- functions from
                    functions.php
                    $path=ExtractFi lePath($filenam e);
                    $i=strrpos($nam e, ".");
                    if($i===false)
                    $files=$name."_ files";
                    else
                    $files=substr($ name, 0, $i)."_files";
                    $filesdir=$path ."\\".$files ;
                    // create directory for
                    if(!file_exists ($filesdir))
                    mkdir($filesdir );
                    >
                    $fFile=fopen($f ilesdir."\\page ".Format($i , 3).".htm", "w");
                    fwrite($fFile, $sSheet_001); <------------ this is where the
                    string is used...
                    // save contens of page
                    for($y=0;$y<cou nt($this->Contens[$i]);$y++)
                    {
                    fwrite($fFile, "<tr>...
                    .....
                    >
                    Does this explain?
                    It does. You variables are not in the methods scope, so they're not
                    available. There are several solutions:

                    1. Define them as constants rather then variables. Constants can be used
                    everywhere.
                    2. Include the file in the method itself.
                    3. Pass the variables in the function/method call.
                    4. Use the global keyword.
                    5. Use the $GLOBALS array.
                    6. Set them as class variables in your class declaration or constructor.

                    4 & 5 are very ugly, which one to use highly depends on how the entier
                    code is supposed to work, wether it should be repeatable, etc.

                    Normally I'd opt for 3 when the the variables are also used elsewhere, and
                    6 if they're only relevant to this class.
                    --
                    Rik Wasmus
                    Posted on Usenet, not any forum you might see this in.
                    Ask Smart Questions: http://tinyurl.com/anel

                    Comment

                    Working...