PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > included file var scope
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
included file var scope

Réponse
 
LinkBack Outils de la discussion
Vieux 07/04/2008, 16h32   #1
Evert Lammerts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut included file var scope

Hi all,

My system is accessible through an index.php file. This file does a
bunch of includes to include global variables, which in function context
become available through the global keyword:

index.php

<?php
require_once "global_vars.php";

function f() {
global $global_var;
var_dump($global_var);
}

f();

?>

However, apart from access through the index.php file I want to be able
to include my index.php file from a function context:

<?php

function includeIndex() {
require_once ("index.php");
}

?>

The logical result is that variables that are globally accessible to
functions when the system is accessed through index.php are now in the
context of includeIndex() - so they're not global.

I want to fix this problem by not having the include inside of the
function context. How to go upon this?

Evert
  Réponse avec citation
Vieux 07/04/2008, 16h46   #2
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] included file var scope

Evert Lammerts wrote:
> My system is accessible through an index.php file. This file does a
> bunch of includes to include global variables, which in function context
> become available through the global keyword:
>
> index.php
>
> <?php
> require_once "global_vars.php";
>
> function f() {
> global $global_var;
> var_dump($global_var);
> }
>
> f();
>
> ?>
>
> However, apart from access through the index.php file I want to be able
> to include my index.php file from a function context:
>
> <?php
>
> function includeIndex() {
> require_once ("index.php");
> }
>
> ?>
>
> The logical result is that variables that are globally accessible to
> functions when the system is accessed through index.php are now in the
> context of includeIndex() - so they're not global.
>
> I want to fix this problem by not having the include inside of the
> function context. How to go upon this?


In index.php rather than declaring vars like so...

$var = 'value';

....declare them in the $GLOBALS array like so...

$GLOBALS['var'] = 'value';

$var is then in the global scope regardless of where it was set.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 07/04/2008, 16h56   #3
Evert Lammerts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] included file var scope


> In index.php rather than declaring vars like so...
>
> $var = 'value';
>
> ...declare them in the $GLOBALS array like so...
>
> $GLOBALS['var'] = 'value';
>
> $var is then in the global scope regardless of where it was set.
>
> -Stut
>


That would work. However I'm looking for a more generic solution,
independent of the system that is being included. So basically I want to
be able to include a file in my function while stepping out of the
context of the function itself.

E
  Réponse avec citation
Vieux 07/04/2008, 16h58   #4
Stut
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] included file var scope

Evert Lammerts wrote:
>
>> In index.php rather than declaring vars like so...
>>
>> $var = 'value';
>>
>> ...declare them in the $GLOBALS array like so...
>>
>> $GLOBALS['var'] = 'value';
>>
>> $var is then in the global scope regardless of where it was set.
>>
>> -Stut
>>

>
> That would work. However I'm looking for a more generic solution,
> independent of the system that is being included. So basically I want to
> be able to include a file in my function while stepping out of the
> context of the function itself.


I'm not sure what you mean. If you're saying you want to include a file
from inside a function but for all parts of it to behave as if it were
being included at the global scope then I don't believe there's a way to
do it.

Maybe it would be better if you tell us exactly what you're trying to
achieve. I don't really see why my solution above would not work so I
probably don't fully understand what you're trying to do.

-Stut

--
http://stut.net/
  Réponse avec citation
Vieux 08/04/2008, 13h28   #5
Németh Zoltán
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] included file var scope

>
>> In index.php rather than declaring vars like so...
>>
>> $var = 'value';
>>
>> ...declare them in the $GLOBALS array like so...
>>
>> $GLOBALS['var'] = 'value';
>>
>> $var is then in the global scope regardless of where it was set.
>>
>> -Stut
>>

>
> That would work. However I'm looking for a more generic solution,
> independent of the system that is being included. So basically I want to
> be able to include a file in my function while stepping out of the
> context of the function itself.
>
> E


put all those variables in a singleton.
then just use MyGlobals::getInstance()->myvar all over the place and you
can do whatever you want with them. of course don't forget to include the
class definition everywhere you want to use it

greets,
Zoltán Németh

>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12543 seconds with 13 queries