problem with using external variable inside class

I have following code:

<?php
$externVar=10;
class Aclass
{
public $a;
function __construct($a)
{
$this->a=$a;
}
public function fun()
{
echo "-->$externVar<--<br/>";
}
}
$obj=new Aclass('sth');
$obj->fun(1);
?>

and on a page I see only --><-- instead -->10<-- which I expect. I
couldn't understand why variable $externVar defined outside class
definition is invisible inside this class. How to use such variables
inside the class definition?
Pawel_Iks [ Do, 17 April 2008 13:23 ] [ ID #1944673 ]

Re: problem with using external variable inside class

On 17 Apr, 12:23, Pawel_Iks <pawel.labed... [at] gmail.com> wrote:
> I have following code:
>
> <?php
> $externVar=10;
> class Aclass
> {
> public $a;
> function __construct($a)
> {
> $this->a=$a;
> }
> public function fun()
> {
> echo "-->$externVar<--<br/>";
> }
> }
> $obj=new Aclass('sth');
> $obj->fun(1);
> ?>
>
> and on a page I see only --><-- instead -->10<-- which I expect. I
> couldn't understand why variable $externVar defined outside class
> definition is invisible inside this class. How to use such variables
> inside the class definition?

http://uk2.php.net/global
Captain Paralytic [ Do, 17 April 2008 13:28 ] [ ID #1944675 ]

Re: problem with using external variable inside class

Captain Paralytic wrote:
> On 17 Apr, 12:23, Pawel_Iks <pawel.labed... [at] gmail.com> wrote:
>> I have following code:
>>
>> <?php
>> $externVar=10;
>> class Aclass
>> {
>> public $a;
>> function __construct($a)
>> {
>> $this->a=$a;
>> }
>> public function fun()
>> {
>> echo "-->$externVar<--<br/>";
>> }
>> }
>> $obj=new Aclass('sth');
>> $obj->fun(1);
>> ?>
>>
>> and on a page I see only --><-- instead -->10<-- which I expect. I
>> couldn't understand why variable $externVar defined outside class
>> definition is invisible inside this class. How to use such variables
>> inside the class definition?
>
> http://uk2.php.net/global
>

Or, better yet, pass it to the function. Globals should be used seldom,
if at all.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Do, 17 April 2008 14:03 ] [ ID #1944679 ]

Re: problem with using external variable inside class

..oO(Pawel_Iks)

>I have following code:
>
><?php
> $externVar=10;
> class Aclass
> {
> public $a;
> function __construct($a)
> {
> $this->a=$a;
> }
> public function fun()
> {
> echo "-->$externVar<--<br/>";
> }
> }
>$obj=new Aclass('sth');
>$obj->fun(1);
>?>
>
>and on a page I see only --><-- instead -->10<-- which I expect. I
>couldn't understand why variable $externVar defined outside class
>definition is invisible inside this class. How to use such variables
>inside the class definition?

In addition to the other replys: You should also fix your error
reporting and set it to E_ALL|E_STRICT in your php.ini, because PHP
would have shown you a notice ("Undefined variable ...").

Micha
Michael Fesser [ Do, 17 April 2008 21:20 ] [ ID #1944709 ]
PHP » comp.lang.php » problem with using external variable inside class

Vorheriges Thema: Re: How to fix mail() double line spacing in message body?
Nächstes Thema: get name of function within function