0

This file I am using but I got this warning.

The script is running okay. But I want to get rid of the message.

The line : $myObj->afstand= $formattedNum . " km"; gives the warning.

I've tried to make a new class, but nothing works.

Does someone know how to get rid of the warning?

 <?php
      $lat1= $_POST['lat1'];
      $lon1= $_POST['lon1'];

      $lat2= $_POST['lat2'];
      $lon2= $_POST['lon2'];


     $lat3= $_POST['lat1'];
     $lon3= $_POST['lon1'];

     $lat4= $_POST['lat2'];
     $lon4= $_POST['lon2'];

    $unit = "K";

    function distance($lat1, $lon1, $lat2, $lon2, $unit) {

           $theta = $lon1 - $lon2;
           $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
           $dist = acos($dist);
           $dist = rad2deg($dist);
           $miles = $dist * 60 * 1.1515;
           $unit = strtoupper($unit);

           if ($unit == "K") {
              return ($miles * 1.609344);
           } else if ($unit == "N") {
              return ($miles * 0.8684);
           } else {
                return $miles;
              }
       }

        $formattedNum = number_format(distance($lat1, $lon1, $lat2, $lon2, "K"), 1);


        $myObj->afstand= $formattedNum . " km";

        $a = $lat3;
        $a .= ",";
        $a .=$lon3;

        $myObj->saddr = $a;

        $myObj->nwlat = $lat3;
        $myObj->nwlon = $lon3;

        $myObj->nwlat2 = $lat4;
        $myObj->nwlon2 = $lon4;

        $myJSON = json_encode($myObj);

        echo $myJSON;
?>

2 Answers 2

2

What is $myObj? I think you forgot to initialize it.

$myObj = new stdClass();

or better define your class with its properties like:

final class MyObj
{
    public $afstand;
    public $saddr;

    public $nwlat;
    public $nwlat2;

    public $nwlon;
    public $nwlon2;
}

Saying that I suggest you improve the names for your properties. What does "afstand" stand for? And what about the "nw" prefix for "lat & lon" properties?

Use intention revealing, pronounceable and searchable names: https://github.com/Chemaclass/php-best-practices/blob/master/technical-skills/meaningful-names.md

Sign up to request clarification or add additional context in comments.

Comments

0

You have forgotten to declare and assign myObj variable.

$myObj = new stdClass();
// $myObj->afstand = '';

1 Comment

Thank you, that was the problem. I don't see the error message again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.