problem with using external variable inside class
am 17.04.2008 13:23:09 von Pawel_Iks
I have following code:
$externVar=10;
class Aclass
{
public $a;
function __construct($a)
{
$this->a=$a;
}
public function fun()
{
echo "-->$externVar<--
";
}
}
$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?
Re: problem with using external variable inside class
am 17.04.2008 13:28:29 von Captain Paralytic
On 17 Apr, 12:23, Pawel_Iks wrote:
> I have following code:
>
>
> $externVar=10;
> class Aclass
> {
> public $a;
> function __construct($a)
> {
> $this->a=$a;
> }
> public function fun()
> {
> echo "-->$externVar<--
";
> }
> }
> $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
Re: problem with using external variable inside class
am 17.04.2008 14:03:18 von Jerry Stuckle
Captain Paralytic wrote:
> On 17 Apr, 12:23, Pawel_Iks wrote:
>> I have following code:
>>
>>
>> $externVar=10;
>> class Aclass
>> {
>> public $a;
>> function __construct($a)
>> {
>> $this->a=$a;
>> }
>> public function fun()
>> {
>> echo "-->$externVar<--
";
>> }
>> }
>> $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@attglobal.net
==================
Re: problem with using external variable inside class
am 17.04.2008 21:20:11 von Michael Fesser
..oO(Pawel_Iks)
>I have following code:
>
>
> $externVar=10;
> class Aclass
> {
> public $a;
> function __construct($a)
> {
> $this->a=$a;
> }
> public function fun()
> {
> echo "-->$externVar<--
";
> }
> }
>$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