简体   繁体   English

如何在php中使用类

[英]How to use class in php

I have this php class and I don't know how to use it. 我有这个php类,我不知道如何使用它。 I know a little bit php but I don't know how to use/call a class. 我知道一点点php,但我不知道如何使用/调用类。

<?php


Class Checkserver {
   static function getString(&$packet){
      $str = "";
      $n = strlen($packet);
      for($i=0;($packet[$i]!=chr(0)) && ($i < $n);++$i)
         $str .= $packet[$i];
      $packet = substr($packet, strlen($str));
      return trim($str);
   }
   static function getChar(&$packet){
      $char = $packet[0];
      $packet = substr($packet, 1);
      return $char;
   }
    function serverInfo($server) {
      list($ip,$port) = explode(":", $server);
      $fp = @fsockopen('udp://'.$ip, $port);
      if($fp) {
         stream_set_timeout($fp, 2);
         fwrite($fp,"\xFF\xFF\xFF\xFFTSource Engine Query\0\r");
         $temp = fread($fp, 4);
         $status = socket_get_status($fp); 
         if($status['unread_bytes']>0) {
            $temp = fread($fp, $status['unread_bytes']);
            $version = ord(self::getChar($temp));
            $array = array();
            $array['status'] = "1";
            if($version == 109) {
               $array['ip'] = self::getString($temp);
               $temp = substr($temp, 1);
               $array['hostname'] = self::getString($temp);
               $temp = substr($temp, 1);
               $array['mapname'] = self::getString($temp);
               $temp = substr($temp, 1);
               self::getString($temp);
               $temp = substr($temp, 1);
               self::getString($temp);
               $temp = substr($temp, 1);
               $array['players'] = ord(self::getChar($temp));
               $array['maxplayers'] = ord(self::getChar($temp));
            } elseif($version == 73) {
               self::getChar($temp);
               $array['hostname'] = self::getString($temp);
               $temp = substr($temp, 1);
               $array['mapname'] = self::getString($temp);
               $temp = substr($temp, 1);
               self::getString($temp);
               $temp = substr($temp, 1);
               self::getString($temp);
               $temp = substr($temp, 3);
               $array['players'] = ord(self::getChar($temp));
               $array['maxplayers'] = ord(self::getChar($temp));
            }
         } else {
            $array['hostname'] = 'Server offline';
            $array['mapname'] = '-';
            $array['players'] = '0';
            $array['maxplayers'] = '0';
            $array['status'] = '0';
         }            
      }
      return $array;


   }


}

?>

I have tried to query a server by writing this in the same file where the class is: 我试图通过在类所在的同一文件中写入服务器来查询服务器:

<?php

   $newServer = serverInfo();
   $date = $newServer->serverInfo("127.0.0.1:27015");
?>

But I can't get it to work. 但是我无法正常工作。 How can I query a server with udp, with this class and print the output ? 如何使用此类用udp查询服务器并输出输出?

serverInfo belongs to the class, so you need to create a new instance of the class before you can use it. serverInfo属于该类,因此您需要先创建该类的新实例,然后才能使用它。

$newServer = new Checkserver();
$date = $newServer->serverInfo("127.0.0.1:27015");

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

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