简体   繁体   English

调用函数时出错

[英]Error When Calling Function

I searched forever trying to find an answer, but was ultimately stumped. 我搜寻了所有试图找到答案的方法,但最终却陷入了困境。 I've been writing code to allow multiple bots to connect to a chat box. 我一直在编写代码,以允许多个漫游器连接到聊天框。 I wrote all the main code and checked it over to make sure it was all okay. 我编写了所有主要代码,并对其进行了检查,以确保一切正常。 Then when I got to calling the function needed to make it work, it gave me an error saying: 然后,当我需要调用使其工作所需的函数时,它给了我一个错误,说:

Notice: Undefined variable: ip in C:\\wamp\\www\\BotRaid.php on line 40 注意:未定义变量:第40行的C:\\ wamp \\ www \\ BotRaid.php中的ip

And also an error saying: 还有一个错误说:

Fatal Error: Cannot access empty property in C:\\wamp\\www\\BotRaid.php on line 40 致命错误:无法访问第40行的C:\\ wamp \\ www \\ BotRaid.php中的空属性

( Also a screenshot here: http://prntscr.com/ckz55 ) (此处还有屏幕截图: 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();

?>

Line 40 is below the "Stop Editing" line. 第40行位于“停止编辑”行下方。 Anyone have any suggestions? 有人有什么建议吗? Or perhaps need me to clear some things up? 还是需要我清理一些东西?

You are accessing the properties of the class incorrectly. 您正在错误地访问该类的属性。

The line: 该行:

$this->connect($this->$ip,$this->$port);

Should be: 应该:

$this->connect($this->ip, $this->port);

Since there was no local variable called $ip , your expression was evaluating to $this-> when trying to access the property since PHP lets you access properties and functions using variables. 由于没有名为$ip局部变量,因此在尝试访问属性时,表达式的计算结果为$this-> ,因为PHP允许您使用变量来访问属性和函数。

For example, this would work: 例如,这将起作用:

$ip = 'ip';
$theIp = $this->$ip; // evaluates to $this->ip

// or a function call
$method = 'someFunction';
$value  = $this->$method(); // evaluates to $this->someFunction();

You will have to change all the occurrences of $this->$foo with $this->foo since you used that notation throughout the class. 因为在整个类中都使用了$this->foo所以必须使用$this->foo来更改$this->foo $this->$foo所有出现。

As noted in the comment by @Aatch, see the docs on variable variables for further explanation. 如@Aatch的注释中所述,请参阅有关变量变量的文档以获取进一步的说明。 But that is what you were running into accidentally. 但这就是您偶然遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM