Jump to content

Quick question about variable scope


hadoob024

Recommended Posts

I'm having a complete brainfart right now and can't seem to remember this.  Tried looking it up in the manual, but couldn't find an example.  Here's what I have:

 

class myclass {

    function testA {
        

    }

    function testB {

    }
}

 

 

So that's my basic setup.  I want to define a variable.  I then want to set it's value in testA().  I then want to be able to access the value in testB().  Where and how do I define this variable?

 

I thought all I had to do was something like:

 

class myclass {

    public $tempvar='';
    
    function testA {
        $tempvar="my new value";

    }

    function testB {
        echo $tempvar;

    }
}

 

But this says that $tempvar is equal to NULL.  What am I forgetting?  Thanks in advance!

Link to comment
Share on other sites

This should do it:

 

<?php
class myclass {

    public $tempvar='';
    
    public function testA() {
        $this->tempvar="my new value";
    }

    public function testB() {
        echo $this->tempvar;
    }
}

$class = new myclass;
$class->testA();
$class->testB();

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.