Problem with class and external variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Problem with class and external variables

Post 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...
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Problem with class and external variables

Post 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;
}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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();
(#10850)
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$this->$var doesn't resolve to anything, $var doesn't exist in that context.
$this->var does.
danf_1979
Forum Commoner
Posts: 72
Joined: Sun Feb 20, 2005 9:46 pm

Post 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.
Post Reply