<?php
include("config.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// connect to the mysql server
$link = mysql_connect($db_host, $db_user, $db_pass)
or die ("nonono ".mysql_error());
// select the database
mysql_select_db($db_name)
or die ("nonono".mysql_error());
class data {
public function getchardata($charname,$opt){
$char = $this->myfetch($this->myquery("select decode(Column1,'decodekey') FROM TableName where Name = '$charname' "));
$blob = $this->strToHex($char[0]);
if($opt =='level')return hexdec(substr($blob,52,2));
if($opt =='coin')return hexdec(substr($blob,26,2).substr($blob,24,2).substr($blob,22,2).substr($blob,20,2));
if($opt =='pet')return hexdec(substr($blob,66,2).substr($blob,64,2).substr($blob,62,2).substr($blob,60,2));
if($opt =='credit')return hexdec(substr($blob,74,2).substr($blob,72,2).substr($blob,70,2).substr($blob,68,2));
if($opt =='exp')return hexdec(substr($blob,44,2).substr($blob,42,2).substr($blob,40,2).substr($blob,38,2).substr($blob,36,2));
}
}
?>
&#13;
我对此函数有问题:在不在对象上下文中时使用$ this。 有人可以帮忙吗? 更新
答案 0 :(得分:0)
我们的想法是将$ this作为方法/函数的参数传递。
因此,要调用您的函数,您将使用类似:getchardata($charname,$opt,$this)
的内容,并在您的函数内部引用$_this
。
<?php
include("config.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// connect to the mysql server
$link = mysql_connect($db_host, $db_user, $db_pass)
or die ("nonono ".mysql_error());
// select the database
mysql_select_db($db_name)
or die ("nonono".mysql_error());
class data {
public function getchardata($charname,$opt,&$_this){
$char = $_this->myfetch($this->myquery("select decode(Column1,'decodekey') FROM TableName where Name = '$charname' "));
$blob = $_this->strToHex($char[0]);
if($opt =='level')return hexdec(substr($blob,52,2));
if($opt =='coin')return hexdec(substr($blob,26,2).substr($blob,24,2).substr($blob,22,2).substr($blob,20,2));
if($opt =='pet')return hexdec(substr($blob,66,2).substr($blob,64,2).substr($blob,62,2).substr($blob,60,2));
if($opt =='credit')return hexdec(substr($blob,74,2).substr($blob,72,2).substr($blob,70,2).substr($blob,68,2));
if($opt =='exp')return hexdec(substr($blob,44,2).substr($blob,42,2).substr($blob,40,2).substr($blob,38,2).substr($blob,36,2));
}
}
?>