Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #6720
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
| Newsgroups | comp.lang.php |
| Subject | Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs |
| Date | Sat, 04 Aug 2012 19:18:12 +0200 |
| Lines | 43 |
| Message-ID | <a853qkFsu5U1@mid.uni-berlin.de> (permalink) |
| References | <583b8426-bd2f-4d85-babb-a3c0630ef3fb@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de OFe1ugMHtdL/QcBplKHj+QStag/12j1t3cFIML0DR57XiZzkPJ9aqL6qGx8E1pm7I= |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120601 Thunderbird/13.0 |
| In-Reply-To | <583b8426-bd2f-4d85-babb-a3c0630ef3fb@googlegroups.com> |
| Xref | csiph.com comp.lang.php:6720 |
Show key headers only | View raw
Am 03.08.2012 19:45, schrieb J. Frank Parnell:
> <?php
> class test{
> function __construct(){
> $this->makevar();
> global $var;
> echo '<hr>__const(): '.$var;
> }
> function makevar(){
> $var = 'something';
> echo '<hr>just declared in makevar(): '.$var;
> global $var;
> echo '<hr>after global in makevar(): '.$var;
> }
> }//class
>
> $t = new test;
> echo '<hr>outside class: '.$var;
> global $var;
> echo '<hr>outside class after global: '.$var;
>
> ?>
> this outputs:
> just declared in makevar(): something
> after global in makevar(): [nothing]
> __const():[nothing]
> outside class: [nothing]
> outside class after global: [nothing]
>
>
> So, 1, why does global $var in the makevar() make it null?
> and B, why dont I have any access to $var in the __constructor or outside the class?
>
Your code is nonsense, you have to declare a variable as global _before_ using it.
> In the real script, $var will be an instance of a different class that I want to use all over the place.
>
Have a look at the singleton pattern.
/Str.
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs "J. Frank Parnell" <juglesh@gmail.com> - 2012-08-03 10:45 -0700
Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-08-04 19:18 +0200
Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs Jerry Stuckle <jstucklex@attglobal.net> - 2012-08-04 14:24 -0400
Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs Jerry Stuckle <jstucklex@attglobal.net> - 2012-08-04 14:45 -0400
Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs "J. Frank Parnell" <juglesh@gmail.com> - 2012-08-04 21:08 -0700
Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs "J.O. Aho" <user@example.net> - 2012-08-05 08:28 +0200
csiph-web