LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-25-2006, 03:14 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
Question php4: problems with variable access in a class method


I have created a class that works as a singleton (though that's not related to the problem). I have noticed that the constructor of the class doesn't get to access the value of a variable that has already been defined in a prevoius include.

It's something like this:

A.php includes B.php (B is the one that defines the variable).
A.php includes singleton.php

then in A.php I try to get an instance of singleton.

Teting, I was able to see that the variable value is reachable outside of the class definition (by testing with echos). But inside the class (in the constructor, to be precise), that variable is empty. What can I do?
 
Old 04-25-2006, 04:15 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
I think that you need to give us a little more to go on. I suspect that it is a problem with the code, so can you post the relevant portions of the code.
 
Old 04-25-2006, 10:26 PM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
Question

Say... it's something like this:

variable.php
Code:
<?
$VARIABLE = "something";
?>
singleton.php
Code:
<?
echo "The outside value of $VARIABLE is " . $VARIABLE . "\n";

class Singleton {

	function Singleton() {
		echo "The inside value of $VARIABLE is " . $VARIABLE . "\n";
	}

	function &getInstance() {
		static $instance;
		
		if (! isset($instance)) {
			$instance = new Singleton();
		}

		return $instance;
	}
}
?>
index.php
Code:
<?

include_once "variable.php";
include_once "singleton.php";

$instance = &Singleton::getInstance();

?>
The output of running index.php would be:
Code:
The outside value of $VARIABLE is something
The inside value of $VARIABLE is
I'm doing it by heart so maybe there's a mistake here or there... but the concept is there.

What do you think?
 
Old 04-25-2006, 10:56 PM   #4
smallville
Member
 
Registered: Dec 2005
Posts: 44

Rep: Reputation: 15
not sure if this will work but try this:

include_once "variable.php";
include_once "singleton.php";

$instance = new Singleton()
$instance -> getInstance();
 
Old 04-25-2006, 11:06 PM   #5
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Original Poster
Rep: Reputation: 83
I'll try tomorrow at the office... but I think it will fail, as it is a static method.
 
Old 04-26-2006, 07:52 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
From the manual on function scope:
Quote:
Any variable used inside a function is by default limited to the local function scope.
That means that your constructor is accessing a local variable called $VARIABLE, since it has not been set up it will echo blank.

If you wish to access $VARIABLE as a global variable then you will need to use the keyword global
Code:
function Singleton() {
   global $VARIABLE;
   echo "The inside value of $VARIABLE is " . $VARIABLE . "\n";
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++: difference between static method variable and static class variable? Thinking Programming 3 01-16-2006 10:08 AM
PHP: how to use a method from a class in the same class ldp Programming 5 09-17-2004 09:52 AM
[JAVA - JLabel] executing a method based on contents of the variable linux_ub Programming 1 08-12-2004 09:50 AM
Java help (accessing array elemonts from another class or method) Tru_Messiah Programming 6 05-14-2004 09:20 AM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 05:11 AM


All times are GMT -5. The time now is 04:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration