Reply to Re: How to keep global strings in another file?

Your name:

Reply:


Posted by Rik on 03/15/07 15:36

Sonnich <sonnich.jensen@elektrobit.com> wrote:

> On Mar 15, 5:34 pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> Sonnichwrote:
>> > On Mar 15, 3:32 pm, Floortje <l...@zingmaarmetmijmee.enel> wrote:
>> >> Sonnichschreef:> On Mar 15, 3:20 pm, Geoff Berrow =

>> <blthe...@ckdog.co.uk> wrote:
>> >>>> Message-ID: =

>> <1173963662.556376.252...@b75g2000hsg.googlegroups.com> =

>> fromSonnichcontained the following:
>> >>>>> something like error messages stored:
>> >>>>> <?
>> >>>>> $sError001=3D"stupid user";
>> >>>>> $sError002=3D"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=3D $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 incl=
ude
>> 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=3D"hello";
>
> //---------------------------------- header for sheets...
> $sSheet_001=3D"blablaabla
>
>
> 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 =3D array();
>
> function SaveFile($filename)
> {
> // extract values for later use
> $name=3DExtractFilename($filename); <--------- functions from
> functions.php
> $path=3DExtractFilePath($filename);
> $i=3Dstrrpos($name, ".");
> if($i=3D=3D=3Dfalse)
> $files=3D$name."_files";
> else
> $files=3Dsubstr($name, 0, $i)."_files";
> $filesdir=3D$path."\\".$files;
> // create directory for
> if(!file_exists($filesdir))
> mkdir($filesdir);
>
> $fFile=3Dfopen($filesdir."\\page".Format($i, 3).".htm", "w");
> fwrite($fFile, $sSheet_001); <------------ this is where the
> string is used...
> // save contens of page
> for($y=3D0;$y<count($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, a=
nd =

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация