|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 ![]() |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Yep, I believe global would work.
PHP: $myVar = new MyClass(); class OtherClass { private $otherVar; function myMethod() { global $myVar; $this->otherVar = $myVar; } } |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 ![]() > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Thanks for all replies..
> Don't let your wishes control you; control your wishes instead. I'll try to think about it ^^ ![]() |
|
![]() |
| Outils de la discussion | |
|
|