here is my code….
class line{
function db($host, $user, $pass, $db){
mysql_connect($host, $user, $pass) or die("Could Not Connect to Database or Database Does not Exists....");
mysql_select_db($db) or die("Database Does not Exists....");
}
$idletime = 300;
$deltime = 600;
function test(){
echo $idletime;
echo idletime;
}
}
and calling this class as
$w = new line();
$w->test();
but it says
Undefined variable: idletime in *****
please let me know what acutally the problem is…
Your variable is a class property — or, at least, I’m guessing that’s what you want it to be…
This line inside your
testmethod :Is trying to access a variable defined inside that method — not a class property.
And there is no such local variable — hence the notice.
To access a class property, you need to use
$this, this way :Also, your code is not valid : you have to declare that your “variables” are indeed class properties — i.e., you need to use some of the visibility keywords in front of them.
Here’s you class, once re-written :
I have :
protected$thisto access them from inside thetestmethodtestmethod ispublic— it’s the default, but I like being explicit about it.Don’t hesitate to spend some time reading the Classes and Objects section of the manual : you’ll learn lots of useful stuff 😉