【问题标题】:Error When Calling Function调用函数时出错
【发布时间】:2012-07-22 06:11:40
【问题描述】:

我一直在寻找答案,但最终被难住了。我一直在编写代码以允许多个机器人连接到聊天框。我编写了所有主要代码并检查了它以确保一切正常。然后,当我调用使其工作所需的函数时,它给了我一个错误提示:

注意:未定义变量:ip in C:\wamp\www\BotRaid.php on line 40

还有一个错误提示:

致命错误:无法访问 C:\wamp\www\BotRaid.php 中的空属性 第 40 行

(这里也是截图: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 行位于“停止编辑”行下方。有人有什么建议吗?或者可能需要我清理一些事情?

【问题讨论】:

    标签: php bots


    【解决方案1】:

    您错误地访问了类的属性。

    行:

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

    应该是:

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

    由于没有名为 $ip 的局部变量,因此您的表达式在尝试访问该属性时计算为 $this-&gt;,因为 PHP 允许您使用变量访问属性和函数。

    例如,这会起作用:

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

    您必须将所有出现的$this-&gt;$foo 更改为$this-&gt;foo,因为您在整个课程中都使用了该符号。

    如@Aatch 评论中所述,请参阅variable variables 上的文档以获取更多说明。但那是你不小心遇到的。

    【讨论】:

    • PHP 的另一个陷阱,变量变量!
    • 啊,谢谢。我已经改变了所有需要改变的东西。然而,我仍然遇到另一个错误。如此处所述:prntscr.com/ckzg8。第 49 行是“$soc = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);”
    • 难道只是if($this-&gt;$soc!=null) socket_close($this-&gt;$soc);还没改成if($this-&gt;soc!=null) socket_close($this-&gt;soc);?第 49 行不应导致未定义的变量 $soc,因为您正在分配它。
    • 哎呀,在那种情况下你是 SOL;您正在运行的 PHP 版本是在不支持套接字的情况下编译的,因此函数 socket_create 在您的 PHP 构建中不存在,您将无法使用它。有关说明,请参阅Sockets - Installation
    • 那时我浪费了好几个小时。哦,好吧,它发生了。我真的很感谢你的帮助。我会看看是否可以找到支持套接字的人来至少为我测试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2014-07-15
    • 2014-04-17
    相关资源
    最近更新 更多