PHP problem

Associate
Joined
29 May 2006
Posts
2,276
Location
Hi all,

Got a problem with some PHP that I can't figure out. I'm sure it's staring me in the face but I'm not seeing it.

I won't post all the code but the jist is this:


PHP:
Class MyClass {

    public $Variable;

    public function A() {

        if(some conditions) {

            // code here etc
            // end result
            $this->Variable = $min($somearray);
        }

        // more code here unrelated to $this->Variable;
    }

    public function B() {

        // loads of code here, none of it touching $this->Variable

        echo $this->Variable;
    }

    public function C() {
        $this->A();
        $this->B();
    }

}

// Go to use the Class

$Obj = new MyClass();
$Obj->C(); // $this->Variable not echo'd
// but if I do this !!
echo $Obj->Variable; // I get the value I want !

For some reason, I can access and return the value of $variable through $Obj but when I use $this->Variable within a method of the class, its not returning a value.

I've checked for typos can't see anything wrong. No synxtax highlighting errors anywhere.

I can't see why it wouldn't work. All the methods are public and so is the variable. :confused:
 
Mmm ok. It works now that I've scrapped B and put the code into A. Thanks. How come it didn't work in the first place? I thought a public variable declared like that could be accessed by any method in the class?

I've set public variables before e.g.

$Obj = new Class();
$Obj->DataVar = array(1,2,3);
$Obj->MethodWhichUsesDataVar(); // some rubbish to do with array is returned.

I've got a solution (Thanks :D) but don't understand what was wrong in the first place :confused:
 
Back
Top Bottom