Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6717 > unrolled thread

Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs

Started by"J. Frank Parnell" <juglesh@gmail.com>
First post2012-08-03 10:45 -0700
Last post2012-08-05 08:28 +0200
Articles 6 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  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

#6717 — Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs

From"J. Frank Parnell" <juglesh@gmail.com>
Date2012-08-03 10:45 -0700
SubjectGlobalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs
Message-ID<583b8426-bd2f-4d85-babb-a3c0630ef3fb@googlegroups.com>
<?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?

In the real script, $var will be an instance of a different class that I want to use all over the place. 
 
thanks, J

[toc] | [next] | [standalone]


#6720 — Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-08-04 19:18 +0200
SubjectRe: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs
Message-ID<a853qkFsu5U1@mid.uni-berlin.de>
In reply to#6717
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.

[toc] | [prev] | [next] | [standalone]


#6721

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-08-04 14:24 -0400
Message-ID<jvjpdl$6m8$1@dont-email.me>
In reply to#6717
On 8/3/2012 1:45 PM, J. Frank Parnell wrote:
> <?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?
>
> In the real script, $var will be an instance of a different class that I want to use all over the place.
>
> thanks, J
>

And what if you have:

$t1 = new test;
$t2 = new test;

Which variable are you supposed to be referencing (they are two 
different variables)?

But what you're doing violates several concepts in OO programming, 
including encapsulation and message passing.  The correct way to do it 
is to pass an object of the class as a parameter (i.e. to the 
constructor) then use getters and setters to reference the variable. 
Proper coding makes your code more reliable, maintainable, reusable and 
easier to understand.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6722

FromJerry Stuckle <jstucklex@attglobal.net>
Date2012-08-04 14:45 -0400
Message-ID<jvjqkl$dru$1@dont-email.me>
In reply to#6721
On 8/4/2012 2:24 PM, Jerry Stuckle wrote:
> On 8/3/2012 1:45 PM, J. Frank Parnell wrote:
>> <?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?
>>
>> In the real script, $var will be an instance of a different class that
>> I want to use all over the place.
>>
>> thanks, J
>>
>
> And what if you have:
>
> $t1 = new test;
> $t2 = new test;
>
> Which variable are you supposed to be referencing (they are two
> different variables)?
>
> But what you're doing violates several concepts in OO programming,
> including encapsulation and message passing.  The correct way to do it
> is to pass an object of the class as a parameter (i.e. to the
> constructor) then use getters and setters to reference the variable.
> Proper coding makes your code more reliable, maintainable, reusable and
> easier to understand.
>

An addition option I forgot to mention - you could also use the 
singleton pattern as M. Strobel referenced.  However, if you do, you 
should always have a default parameter in the call so you can use 
something other than the singleton when you need to.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#6723

From"J. Frank Parnell" <juglesh@gmail.com>
Date2012-08-04 21:08 -0700
Message-ID<183a6ce7-9ac7-4b98-9bc3-68d9eb895501@googlegroups.com>
In reply to#6722
On Saturday, August 4, 2012 11:45:35 AM UTC-7, Jerry Stuckle wrote:
> On 8/4/2012 2:24 PM, Jerry Stuckle wrote:
> 
> > On 8/3/2012 1:45 PM, J. Frank Parnell wrote:
> 
> >> <?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?
> 
> >>
> 
> >> In the real script, $var will be an instance of a different class that
> 
> >> I want to use all over the place.
> 
> >> thanks, J
> 
> >>
> 
> >
> 
> > And what if you have:
> 
> >
> 
> > $t1 = new test;
> 
> > $t2 = new test;
> 
> >
> 
> > Which variable are you supposed to be referencing (they are two
> 
> > different variables)?
> 
> >
> 
> > But what you're doing violates several concepts in OO programming,
> 
> > including encapsulation and message passing.  The correct way to do it
> 
> > is to pass an object of the class as a parameter (i.e. to the
> 
> > constructor) then use getters and setters to reference the variable.
> 
> > Proper coding makes your code more reliable, maintainable, reusable and
> 
> > easier to understand.
> 
> >
> 
> 
> 
> An addition option I forgot to mention - you could also use the 
> 
> singleton pattern as M. Strobel referenced.  However, if you do, you 
> 
> should always have a default parameter in the call so you can use 
> 
> something other than the singleton when you need to.

Ok, I read a little on the singleton, that does seem like a good way of doing it.  What I wound up doing, before you guys replied, was just $this->var = 'something' and then doing $t->var where I needed that 'something'.  Which I think make sense for OO? Because it is somewhat logical for 'something' to be part of my class, in my situation.  Although 'something' is a different object, eg, an api class. My class is a child class of a controller in an MVCish framework.

I just found out that this works
class test{
	function __construct(){
		$this->makevar();
	}
	function makevar(){
	global $var;
		$var = 'something';// actually, $var = new something();
	}	
}//class

$t = new test;
echo '<hr>outside class: '.$var;//works  And I assume would work in a unrelated function if I globalled it in there.
 
> always have a default parameter in the call so you can use
> something other than the singleton when you need to

not sure what this means

[toc] | [prev] | [next] | [standalone]


#6724 — Re: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs

From"J.O. Aho" <user@example.net>
Date2012-08-05 08:28 +0200
SubjectRe: Globalizing vars in class methods doesnt seem to work, var disappears after global, var is inaccessible in other methods/funcs
Message-ID<a86i3iFldeU1@mid.individual.net>
In reply to#6723
On 05/08/12 06:08, J. Frank Parnell wrote:
> On Saturday, August 4, 2012 11:45:35 AM UTC-7, Jerry Stuckle wrote:
>> On 8/4/2012 2:24 PM, Jerry Stuckle wrote:
>
>>
>> An addition option I forgot to mention - you could also use the
>>
>> singleton pattern as M. Strobel referenced.  However, if you do, you
>>
>> should always have a default parameter in the call so you can use
>>
>> something other than the singleton when you need to.
>
> Ok, I read a little on the singleton, that does seem like a good way of doing it.  What I wound up doing, before you guys replied, was just $this->var = 'something' and then doing $t->var where I needed that 'something'.  Which I think make sense for OO? Because it is somewhat logical for 'something' to be part of my class, in my situation.  Although 'something' is a different object, eg, an api class. My class is a child class of a controller in an MVCish framework.
>
> I just found out that this works
> class test{
> 	function __construct(){
> 		$this->makevar();
> 	}
> 	function makevar(){
> 	global $var;
> 		$var = 'something';// actually, $var = new something();
> 	}	
> }//class
>
> $t = new test;
> echo '<hr>outside class: '.$var;//works  And I assume would work in a unrelated function if I globalled it in there.
>
>> always have a default parameter in the call so you can use
>> something other than the singleton when you need to
>
> not sure what this means

class test {
	private $var = null;

	function __construct($invalue = null) {
		$this->var = $invalue;
	}
}

IMHO it's better to have functions if you want to manipulate values in a 
class, there are many ways to do it, but the simplest would just be:

	function getVar() {
		return $this->var;
	}

	function setVar($invalue) {
		// verify that the input is of the right form
		// then update the value
		$this->var = $invalue;
	}

-- 

  //Aho

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web