无法将PHP错误对象转换为字符串
我试图使用我创建的类的get方法,错误是 “可捕获的致命错误:类IdmoreRO的对象无法在中转换为字符串” 当我尝试使用magic方法时,它的错误也是 致命错误:方法IdmoreRO::\uu tostring()无法在中接受参数 这是我的密码: idmore.php无法将PHP错误对象转换为字符串,php,Php,我试图使用我创建的类的get方法,错误是 “可捕获的致命错误:类IdmoreRO的对象无法在中转换为字符串” 当我尝试使用magic方法时,它的错误也是 致命错误:方法IdmoreRO::\uu tostring()无法在中接受参数 这是我的密码: idmore.php class IdmoreRO { public function __construct() { } //hitung ongkir public function __toString
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;
正如错误消息所说,
\uuu toString
无法接收任何参数
你也许可以这样做
class IdmoreRO
{
private $origin;
private $destination;
private $weight;
private $courier;
public function __construct(
$origin,
$destination,
$weight,
$courier
) {
$this->origin = $origin;
$this->destination = $destination;
$this->weight = $weight;
$this->courier = $courier;
}
public function __toString()
{
// use $this->origin, $this->destination, $this->weight and $this->courier
}
}
正如错误消息所说,
\uuu toString
无法接收任何参数
你也许可以这样做
class IdmoreRO
{
private $origin;
private $destination;
private $weight;
private $courier;
public function __construct(
$origin,
$destination,
$weight,
$courier
) {
$this->origin = $origin;
$this->destination = $destination;
$this->weight = $weight;
$this->courier = $courier;
}
public function __toString()
{
// use $this->origin, $this->destination, $this->weight and $this->courier
}
}
不要在参数中使用
\uuu toString
,这是一种将对象自动转换为字符串的神奇方法。你应该把你的函数命名为其他名称。可能的重复项不要使用参数\uuuu toString
,它是一种神奇的方法,可以自动将对象转换为字符串。您应该将函数命名为其他名称。函数可能重复