LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php4: problems with variable access in a class method (https://www.linuxquestions.org/questions/programming-9/php4-problems-with-variable-access-in-a-class-method-438753/)

eantoranz 04-25-2006 03:14 PM

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? :scratch:

graemef 04-25-2006 04:15 PM

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.

eantoranz 04-25-2006 10:26 PM

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?

smallville 04-25-2006 10:56 PM

not sure if this will work but try this:

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

$instance = new Singleton()
$instance -> getInstance();

eantoranz 04-25-2006 11:06 PM

I'll try tomorrow at the office... but I think it will fail, as it is a static method.

graemef 04-26-2006 07:52 AM

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";
}



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