【发布时间】:2016-11-18 07:18:49
【问题描述】:
我尝试使用我创建的类中的get方法,错误提示
“可捕获的致命错误:无法将 IdmoreRO 类的对象转换为字符串”
当我尝试使用魔术方法 __toString() 时,它的错误也会说
致命错误:方法 IdmoreRO::__tostring() 不能接受参数
这是我的代码:
idmore.php
class IdmoreRO
{
public function __construct()
{
}
//hitung ongkir
public function __toString($origin,$destination,$weight,$courier)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://rajaongkir.com/api/starter/cost",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "origin=$origin&destination=$destination&weight=$weight&courier=$courier",CURLOPT_HTTPHEADER => array("key: $this-> 3f01f13ce2b42ba983ad3f3bc4852f84"),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
$result = 'error';
return 'error';
} else {
return $response;
}
}
}
进程.php
#header("Content-Type: application/x-www-form-urlencoded");
require_once('idmore.php');
$IdmoreRO = new IdmoreRO();
if(isset($_GET['act'])):
switch ($_GET['act']) {
case 'showprovince':
$province = $IdmoreRO->showProvince();
echo $province;
break;
case 'showcity':
$idprovince = $_GET['province'];
$city = $IdmoreRO->showCity($idprovince);
echo $city;
break;
case 'cost':
$origin = $_GET['origin'];
$destination = $_GET['destination'];
$weight = $_GET['weight'];
$courier = $_GET['courier'];
$cost = $IdmoreRO->__toString($origin,$destination,$weight,$courier);
echo $cost;
break;
}
endif;
【问题讨论】:
-
不要使用
__toString带参数,它是一种自动将对象转换为字符串的神奇方法。您应该将您的函数命名为其他名称。
标签: php