0

I passed an object in a function in laravel. I tried to assign the value of a property to a variable. It pops an error.

When I tried to figure out what was going on I tried to dd ()the value it worked.

But when I tried to echo the same it does not.

What am I mising.

On using this,

location1 and location2 are two objects of the location class.

The function is here:

function geodistance($location1,$location2){
    dd($location1->lat);
    $lat1=$location1->lat;
}

it prints

"28.612072"

But when I change the same function to

function geodistance($location1,$location2){
    echo($location1->lat);
    $lat1=$location1->lat;
}

The output error is:

Trying to get property 'lat' of non-object

Even the function

function geodistance($location1,$location2){
echo $location1->lat;
$lat1=$location1->lat;
}

gives the same output

The aim to remind you is to assign the value to a variable like so.

function geodistance($location1,$location2){
    $lat1=$location1->lat;
}

When I echo $location1 from within the function geodistance() gives out and error which makes sense.

function geodistance($location1,$location2){
    echo $location1;
    $lat1=$location1->lat;

Object of class App\MyClasses\city could not be converted to string

When I dd ($location1 from the funciton like so it gives the right result.

function geodistance($location1,$location2){
  dd($location1);
  $lat1=$location1->lat;

like so:

city {#13410 ▼ +id: 2245 +info: "{}"
+name: "New Delhi"
+lat: "28.612072"
+lon: "77.22978"
+timezone: null
+weightedrating: null +country_id: 1 }

It seems i am missing out something very trivial. :(

16
  • can you provide the code where you call both geodistance() functions
    – Prodigle
    Aug 13, 2018 at 11:43
  • 1
    are you passing array as parameters in function? Aug 13, 2018 at 12:18
  • 4
    dd will dump the variable and terminate the script but echo will not terminate, which may mean that $location1 is an object the first time the method is called but not every time the method is called.
    – apokryfos
    Aug 13, 2018 at 12:42
  • 1
    Have you tried echo $location1->lat; ? (no parenthesis) Also consider apokyfos notes, and the fact that not immediately terminating the script might turn difficult to find your printed value after script termination view rendering.
    – alariva
    Aug 13, 2018 at 14:36
  • @apokryfos: NO the error is being displayed in laravel with the right line number.: Yes. Updated the results. Aug 13, 2018 at 17:16

1 Answer 1

0

As pointed out by @salman zafar and @apokryfos, the mistake was that I was passing an array.

When I was doing a dd, after processing the first data the programme was terminated.

The array that was passed was not completely correct. The last data point in my array was not an object of the same class.

So the data ended up being corrupted when using echo. Echo would print one data after another but eventually it would get a wrong input.

The best way to catch these errors (after so much experimenting) is to write the values of the variable in a text file on each iterartion. What that does is that it catches the error in your text file and u get to know after how many iterations the programme stopped.

Hope this helps others from making such obviously glaring mistakes which are tough to catch (atleast for me since this was the first of such errors)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.