Page 1 of 1
Problem with class and external variables
Posted: Mon Jan 30, 2006 2:46 pm
by danf_1979
I'm just starting to code a simple class to test some features. I have this code:
Code: Select all
<?php
$var = "TEST";
class test {
function test() {
print $var;
}
}
$test = new test
?>
The problem is that I want to print $var in the class, but if I use global $var it gives me an error, so I guess global its not the way to go. Any hints?
Thanks...
Re: Problem with class and external variables
Posted: Mon Jan 30, 2006 2:55 pm
by alex.barylski
danf_1979 wrote:I'm just starting to code a simple class to test some features. I have this code:
Code: Select all
<?php
$var = "TEST";
class test {
function test() {
print $var;
}
}
$test = new test
?>
The problem is that I want to print $var in the class, but if I use global $var it gives me an error, so I guess global its not the way to go. Any hints?
Thanks...
I believe you have to bring the global variables into local scope...
Code: Select all
function test()
{
global $var;
echo $var;
}
Posted: Mon Jan 30, 2006 2:58 pm
by alex.barylski
Just a side note...
When the whole idea of OOP is to kinda get away from messy globals...
Thats not to say GLOBALS are obsolete, but OOP was primarily invented to support re-use and encapsulation...something GLOBALS don't lend themselves to very nicely
So in short, try and avoid using GLOBALS inside classes...use member variables instead and try not to access data directly, but rather through getter/setter functions...
Cheers

Posted: Mon Jan 30, 2006 3:07 pm
by danf_1979
Thanks both. That code worked.
Yes, I'm trying to implement some class interface right now, but my code is not quite OK for a 100% class methods. I'll need to work a lot on it, so right now I'm just trying to make a class to just avoid repeating code.
Posted: Mon Jan 30, 2006 3:25 pm
by Jenk
In that method it needs global, or to atleast be in a global variable.
Quite a number of developers make their own mock-superglobal, and use this to access variables at all levels. (simply create an array and use it like you would $_SERVER, or $_GET for example, but you must use 'global' when in local scope of a function or class)
Another method is to create a registry class, or (singleton) object.
Posted: Mon Jan 30, 2006 3:59 pm
by Christopher
Better would be to initialize the object with the constructor:
Code: Select all
$var = "TEST";
class test {
var $var;
function test($var) {
$this->var = $var;
}
function print() {
print $this->var;
}
}
$test = new test($var);
$test->print();
Posted: Sun Feb 05, 2006 8:13 pm
by danf_1979
Ok thanks. Why this does not work?
Code: Select all
<?php
class test {
var $var="string";
function my_print() {
echo $this->$var;
}
}
$test = new test;
$test->my_print();
?>
But if i set $test->$var = "string"; outside the class then works. How can I manage to get a working value of $var from inside the class being echoed? I know this is very basic, but I'm just starting on classes in php.
Posted: Sun Feb 05, 2006 8:17 pm
by feyd
$this->$var doesn't resolve to anything, $var doesn't exist in that context.
$this->var does.
Posted: Sun Feb 05, 2006 8:20 pm
by danf_1979
Ok perfect. Thank you so much. It has some time since I didnt get help from you. You helped me a lot with a phpnuke website last year, but I dont espect you to remember ofcourse. I learned some php with phpnuke, then learned python and abandoned php and now I'm back heh. Cheers.