0

I tried to use get method from class i created, the error says

"Catchable fatal error: Object of class IdmoreRO could not be converted to string in"

when i tried to use magic method __toString() its error too says

Fatal error: Method IdmoreRO::__tostring() cannot take arguments in

here's my code :

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;
Gusti Aldi
  • 203
  • 1
  • 5
  • 15
  • 2
    Don't use `__toString` with parameters, it's a magic method to automatically convert objects into strings. You should just name your function something else. – Dave Chen Nov 18 '16 at 07:21
  • Possible duplicate of [Can I bring back old \_\_tostring() behaviour in PHP 5.3?](http://stackoverflow.com/questions/5260607/can-i-bring-back-old-tostring-behaviour-in-php-5-3) – insertusernamehere Nov 18 '16 at 07:28

1 Answers1

3

As the error message says, __toString can not receive any argument

You could probably do something like this

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
    }
}
marcosh
  • 8,780
  • 5
  • 44
  • 74