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!
Alright, I put all my news functions into a class and I just have a quick question. would I declare the variables I want to use in every function like this?:
The only problem is that I want to use all those values in more than one function so I don't want to declare them each time I want to call a function. I want them to be kind of like global variables throughout each function.
if you declare a local variable inside a method they're local only. To share them with the object (and other members) you have to use the $this->var syntax, and you should declare them at the beginning as mentor suggested.
class test
{
var $objectVar = null;
function test()
{
$localVar = 'bob'; // isolated local variable
$this->objectVar = 'frank'; // available to all methods as well as externally
echo $localVar.' spanks '.$this->objectVar;
}
}
$t = new test;
$t->test(); // prints 'bob spanks frank'
echo $t->objectVar; // prints 'frank'
I've been doing that. I set all var = null; then give values to them in a function as $this->year = $_POST['year']; but when I go to use $this->year in another function it doesn't return anything.