PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > accessing vars from within classes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
accessing vars from within classes

Réponse
 
LinkBack Outils de la discussion
Vieux 12/09/2007, 19h03   #1
PaowZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut accessing vars from within classes

Hello there!

I'd like to make some declared vars available to classes, such as:

<? //main environment
$myVar = new MyClass(); //the var I want to be available to OtherClass
....
....
class OtherClass{
private $otherVar = $myVar; //seems to not work
...
}
?>

...and I wish to initialize $otherVar 'as is', I mean, without having
to pass arg through __construct(). The way I tried above doesnt seem
to work, unlike it would be in a function..

any idea ??

thanks a lot

  Réponse avec citation
Vieux 12/09/2007, 20h47   #2
ragearc@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing vars from within classes

Yep, I believe global would work.

PHP:

$myVar = new MyClass();

class OtherClass {

private $otherVar;

function myMethod() {
global $myVar;
$this->otherVar = $myVar;
}
}

  Réponse avec citation
Vieux 12/09/2007, 20h53   #3
Shelly
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing vars from within classes

I have a simple question, You are using classes because you want to be OO
rather than procedural. Why, then, would you expect what you did to work.
Instead, OtherClass should have accessor and mutator methods. It would bebe
executed as $otherClassInstance.setOtherVar($myVar) where
$otherClassInstance is and instance of OtherClass.

So, in summary, if you are procedual then stay procedural and if you are OO
then stay OO.



"PaowZ" <gpaowz@gmail.com> wrote in message
news:1189616607.313148.113350@w3g2000hsg.googlegro ups.com...
> Hello there!
>
> I'd like to make some declared vars available to classes, such as:
>
> <? //main environment
> $myVar = new MyClass(); //the var I want to be available to OtherClass
> ...
> ...
> class OtherClass{
> private $otherVar = $myVar; //seems to not work
> ..
> }
> ?>
>
> ..and I wish to initialize $otherVar 'as is', I mean, without having
> to pass arg through __construct(). The way I tried above doesnt seem
> to work, unlike it would be in a function..
>
> any idea ??
>
> thanks a lot
>



  Réponse avec citation
Vieux 13/09/2007, 00h45   #4
NC
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing vars from within classes

On Sep 12, 10:03 am, PaowZ <gpa...@gmail.com> wrote:
>
> $myVar = new MyClass(); //the var I want to be available to OtherClass
> ...
> ...
> class OtherClass{
> private $otherVar = $myVar; //seems to not work
> ..}


But of course. The scope of $myVar is lies outside OtherClass, so
$myVar is meaningless in the context of OtherClass.

> ..and I wish to initialize $otherVar 'as is', I mean,
> without having to pass arg through __construct().


And I wish to build a perpetual motion machine. Alas, the world does
not work that way...

> The way I tried above doesnt seem to work, unlike it would
> be in a function..


It wouldn't work in a function either, unless you declare $myVar as
global.

> any idea ??


Don't let your wishes control you; control your wishes instead.

Cheers,
NC

  Réponse avec citation
Vieux 13/09/2007, 16h13   #5
PaowZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: accessing vars from within classes

Thanks for all replies..

> Don't let your wishes control you; control your wishes instead.

I'll try to think about it ^^


  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 01h37.


É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,10218 seconds with 13 queries