Tanımlanmamış değişken: boszaman

2 Cevap

İşte benim kod ....

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

}

ve bu sınıf çağrı

$w = new line();
$w->test();

ama diyor

Tanımlanmamış değişken: * içinde boszaman

Bana acutally sorunun ne olduğunu bildirin lütfen ...

2 Cevap

Değişken bir class property -- or, at least, I'm guessing that's what you want it to be... olduğunu


This line inside your test method :

echo $idletime;

Bu yöntem içinde tanımlanan bir değişkene erişmeye çalışıyor -- not a class property.

Dolayısıyla haber - Ve böyle bir lokal değişken var.


To access a class property, you need to use $this, this way :

echo $this->idletime;


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.

İşte size class kez yeniden yazılmış bulunuyor:

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

    protected $idletime = 300;
    protected $deltime = 600;

    public function test(){
        echo $this->idletime;
    }
}

I var:

  • protected gibi özelliklerini ayarlama
  • Ve ben test yöntemi içinde bunlara erişmek için $this istimal
  • Ben de test yöntemi public olduğunu belirttiler - varsayılan, ama ben bu konuda açık olmak istiyorum.


Don't hesitate to spend some time reading the Classes and Objects section of the manual : you'll learn lots of useful stuff ;-)

Eğer işlev çağrısı değişkenleri geçmesi gerekiyor,

function test($idletime){
    echo $idletime;
    echo idletime;
}

Ya da değişken kapsamı küresel ilk olarak onları tanımlamak dışında olduğunu olabilir