[英]Error When Calling Function
我搜尋了所有試圖找到答案的方法,但最終卻陷入了困境。 我一直在編寫代碼,以允許多個漫游器連接到聊天框。 我編寫了所有主要代碼,並對其進行了檢查,以確保一切正常。 然后,當我需要調用使其工作所需的函數時,它給了我一個錯誤,說:
注意:未定義變量:第40行的C:\\ wamp \\ www \\ BotRaid.php中的ip
還有一個錯誤說:
致命錯誤:無法訪問第40行的C:\\ wamp \\ www \\ BotRaid.php中的空屬性
(此處還有屏幕截圖: http : //prntscr.com/ckz55 )
<?php
date_default_timezone_set("UCT");
declare(ticks=1);
set_time_limit(0);
class BotRaid
{
public $ip="174.36.242.26";
public $port=10038;
public $soc = null;
public $packet = array();
##############################
# You can edit below this #
##############################
public $roomid="155470742";
public $userid = "606657406";
public $k = "2485599605";
public $name="";
public $avatar=;
public $homepage="";
##############################
# Stop editing #
##############################
public function retry()
{
$this->connect($this->$ip,$this->$port); //Line 40, where I'm getting the error now.
$this->join($this->$roomid);
while($this->read()!="DIED");
}
public function connect($ip, $port)
{
if($this->$soc!=null) socket_close($this->$soc);
$soc = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if(!$this->$soc)$this->port();
if(!socket_connect($this->$soc,$this->$ip,$this->$port))$this->port();
}
public function port()
{
$this->$port++;
if($this->$port>10038) $this->$port=10038;
$this->retry();
}
public function join($roomid)
{
$this->send('<y m="1" />');
$this->read();
$this->send('<j2 q="1" y="'.$this->$packet['y']['i'].'" k="'.$this->$k.'" k3="0" z="12" p="0" c"'.$roomid.'" f="0" u="'.$this->$userid.'" d0="0" n="'.$this->$name.'" a="'.$this->$avatar.'" h="'.$this->$homepage.'" v="0" />');
$this->port();
$this->$roomid;
}
public function send($msg)
{
echo "\n Successfully connected.";
socket_write($this->$soc, $this->$msg."\0", strlen($this->$msg)+1);
}
public function read($parse=true)
{
$res = rtrim(socket_read($this->$soc, 4096));
echo "\nSuccessfully connected.";
if(strpos(strtolower($res), "Failed"))$this->port();
if(!$res) return "DIED";
$this->lastPacket = $res;
if($res{strlen($res)-1}!='>') {$res.=$this->read(false);}
if($parse)$this->parse($res);
return $res;
}
public function parse($packer)
{
$packet=str_replace('+','@più@',str_replace(' ="',' @=@"',$packet));
if(substr_count($packet,'>')>1) $packet = explode('/>',$packet);
foreach((Array)$packet as $p) {
$p = trim($p);
if(strlen($p)<5) return;
$type = trim(strtolower(substr($p,1,strpos($p.' ',' '))));
$p = trim(str_replace("<$type",'',str_replace('/>','',$p)));
parse_str(str_replace('"','',str_replace('" ','&',str_replace('="','=',str_replace('&','__38',$p)))),$this->packet[$type]);
foreach($this->packet[$type] as $k=>$v) {
$this->packet[$type][$k] = str_replace('@più@','+',str_replace('@=@','=',str_replace('__38','&',$v)));
}
}
}
}
$bot = new BotRaid; //This is where I had the error originally
$bot->retry();
?>
第40行位於“停止編輯”行下方。 有人有什么建議嗎? 還是需要我清理一些東西?
您正在錯誤地訪問該類的屬性。
該行:
$this->connect($this->$ip,$this->$port);
應該:
$this->connect($this->ip, $this->port);
由於沒有名為$ip
局部變量,因此在嘗試訪問屬性時,表達式的計算結果為$this->
,因為PHP允許您使用變量來訪問屬性和函數。
例如,這將起作用:
$ip = 'ip';
$theIp = $this->$ip; // evaluates to $this->ip
// or a function call
$method = 'someFunction';
$value = $this->$method(); // evaluates to $this->someFunction();
因為在整個類中都使用了$this->foo
所以必須使用$this->foo
來更改$this->foo
$this->$foo
所有出現。
如@Aatch的注釋中所述,請參閱有關變量變量的文檔以獲取進一步的說明。 但這就是您偶然遇到的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.