0
我试图用get方法从类我创建的错误说PHP错误对象无法转换为字符串
“开捕致命错误:类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;
}
}
}
process.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 5.3中带回旧\ _ \ _ tostring()行为吗?](http://stackoverflow.com/questions/5260607/can-i-bring-back-old-tostring-behaviour-in -php-5-3) – insertusernamehere