Trying to get property 'totalInterestPaid' of non-object when object is present - php

I have never come across a issue quite like this. Laravel is saying that I am trying to get a property of a non-object when the variable I am referring to is indeed a object.
$balanceToPush = DB::table('loan_balances')
->where('loansID', $loan->id )
->first();
$balanceToPush->totalPaid = number_format($balanceToPush->totalInterestPaid + $balanceToPush->totalTaxPaid + $balanceToPush->totalPrincipalPaid, 2, '.', ',');
//dd($balanceToPush);
array_push($loanBalancesArray, $balanceToPush);
The above code fails at the first variable in the number formatter $balanceToPush. I can get rid of the first two and just have, for example, $balanceToPush->totalTaxPaid and it will throw the same error.
However, when I dump the $balanceToPush variable it definitely is a object with the fields I am trying to access:
{#1249 ▼
+"id": 2
+"loansID": 2
+"loansBalance": 9000.0
+"totalInterestPaid": 1670.0
+"totalPrincipalPaid": 1000.0
+"totalTaxPaid": 551.1
+"created_at": "2021-09-16 10:12:27"
+"updated_at": "2021-09-16 10:12:27"
+"totalPaid": "3,221.10"
}
What I have tried?
At this point, I copy and pasted the exact variables from the dd($balanceToPush) and pasted them into the code. Same issue.
I tried to clear the cash by doing php artisan clear:cache, still the same thing.
The most confusing part of this all, was that it was all working fine throughout the day. I then took a break for a few hours came back and then this error popped up.
All help is appreciated.

This is not really Laravel that is complaining, this is PHP itself.
Althrough it can be confusing, it is very very very unlikely PHP is making a mistake, and you can assume that at some point, you are indeed trying to get a property from a non object.
One of the common mistakes is to use dd in a loop (don't know if that's the case here, but if it is, then you only "test" the first iteration then the script stops).
Try this, I rearranged your code so it is easier to debug:
dump($loan); //the same output as dd, but without stopping the script
$balanceToPush = DB::table('loan_balances')
->where('loansID', $loan->id )
->first();
dump($balanceToPush);
//$balanceToPush->totalPaid = number_format($balanceToPush->totalInterestPaid + $balanceToPush->totalTaxPaid + $balanceToPush->totalPrincipalPaid, 2, '.', ',');
array_push($loanBalancesArray, $balanceToPush);
You should get a null output at some point. If it works, try again but uncomment the number_format part.
If you are still getting the same error without any null values in the dumps, can you edit your question with the entire exception trace and a wider portion of your code please?

Related

Extracting consecutive array elements except the first in PHP without receiving weird error

Until recently, I have unused variables as a result of using list() to extract only certain elements from an array. I'm trying to do a quick function that retrieves the elements.
Let's say for example, I have a function I call with parameters and it always returns the following in an array
Index 0: John Doe
Index 1: 123 Jane Street
Index 2: 902-555-5555
Index 3: Canada
If I wanted all the elements, I can easily do something like:
list($name,$address,$phone,$country)=functiontogetinfo();
And then I can run it through an unused variable tester like PHPMD and it will be happy. However, if I decide to use only the last three values (address, phone, country), then PHPMD will complain about $name being an unused variable. This bugs me because I begin to think unused allocated variables waste memory.
I then tried some ways to make things easier both on PHPMD and the computer memory. I tried this on a command line:
php -r "list(NULL,$a)=x();echo $a;exit();function x(){return array(1,2);};"
I was hoping a "2" would be shown as a result, but instead I get a strange error of:
Parse error: syntax error, unexpected ',', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in Command line code on line 1
If I changed NULL to any variable, the code would compile fine but then PHPMD would complain about an unused variable.
Is it possible for me to get all the elements of an array except for the first one without having to try to go through each index manually and without receiving that weird error?
nevermind. I found out online that I simply remove the variable and use nothing in its place. for example:
list(,$var2)
instead of
list(NULL,$var2)

Stored value from soap __getLastRequest() echos but won't register in array

I've got a problem that seems particularly odd (to me at least). So I'm using a soap API to interact with a program that (among other things) creates user groups and assigns users to them. I need to create a group and then assign a user to it using two different soap functions; this requires me to store the returned variable from one soap api call and input it into the next (see below).
$value = $SOAP->addOrganisation($params);
$group=$SOAP->__getLastResponse();
echo "<br/>Group: ".$group."<br/>";
$params=array('SID'=>$sid, 'user_id'=>$user, 'organisation_id'=>$group, 'insertedby'=>1);
$value= $SOAP->addUserToOrganisation($params);
Straight-forward right?
But here's where it gets interesting: the variable $group echos what I'm expecting but seems not to work in the soap call addUserToOrganisation. The returned value says that it ran fine, however addUserToOrganisation doesn't end up carrying out its task (actually adding the user to the group).
Conjectures anticipated: (Stuff I've already guessed at)
Maybe it's a glitch in the function addUserToOrganisation?
That would make sense and I thought the same. However when I hard-coded a number in there (even when it was the exact same number the variable would have echoed), it works just fine.
Maybe the API doesn't play nicely with variables in that spot in the array?
I actually reset the variable manually with a number (see below) and it worked no problem.
$value = $SOAP->addOrganisation($params);
$group=$SOAP->__getLastResponse();
echo "<br/>Group: ".$group."<br/>";
$group=55;
echo "<br/>Group: ".$group."<br/>";
$params=array('SID'=>$sid, 'user_id'=>$user, 'organisation_id'=>$group, 'insertedby'=>1);
$value= $SOAP->addUserToOrganisation($params);
Any stray spaces attached to that variable?
None that I can see.
...Maybe you pissed off the gnomes inside your computer that make it work?
Quite possibly but they've put up with my abuse for so long, why the sudden uprising?
My best guess: As far as I can tell, there just seems that there is something inherently unacceptable about using the output of __getLastRequest(), even though it just looks like a regular old number when echoed. I don't know if not all strings are equal or something but if any of you have an idea, I'd really appreciate it.
(just watch, it turns out to somehow be some dumb syntax error)
Anyway, thanks in advance

Error: [ ] operator not supported for strings, when attempting to append to an array

So I have a script, thats a handful of if-else statements. That if there is an error somewhere It will append a message to an array. Since there can be a good handful of errors for whatever case. I dump them in this array to later list them to the user.
$outputMsg = array();
$outputMsg[] = "State not selected";
Which this worked just fine up until today, which at random I start getting this error.
[] operator not supported for strings
I can't figure it out.
As pointed out in the comments.. upon a deeper inspection of my code, it turns out that my array is being converted into a string, and despite that notion continues on, where once upon hitting another error to append to the array, that error occurs.
I appreciate the help. But it is now resolved I believe, if not, I know what I'm looking for now. The problem was about 100 lines up from where the state error is, someone added in a new error for a new field, and didn't make it append to the array in a similar fashion, they just made it a string.

Find code line or file where php parameter is set

I have an old application witch pops up an error at a certain location. The error is about an wrong set variable. Only from the error it is not possible to find the location where the variable is set wrong. Now my idea is to use reflections to find the location.
Is it possible to use reflections to find the code position at which a variable gets a certain value?
The idea: I have the name and the value of the variable. Now if both are matching a certain event should be triggered and echo the actual parsed file and line number.
Every ideas that help are appreciated.
Thank you,
-lony
P.S.: Is it possible even if the application is not really object oriented and uses a lot of spaghetti code?
I would be you do a debug_backtrace at the point where the error occurs and try to exploit the stack trace to see where the variable is changed. The debug_backtrace would give you a list of file included after it should be fairly easy to filter a list of line with a global search (i.e. grep)
var_dump(debug_backtrace())
if (variable == value) {
echo "variable equals value, line #whatever"+"<br/>";
}
Just place these at various points in code and see which ones display. Manually enter line numbers.
I found a solution to one of my problems.
The function debug_print_backtrace helped me finally debugging my spaghetti code. I found it by reading this post.
-Cheers

PHP - Problem with scalar value and memory leak

I am dealing with a very strange situation that has to do with the Warning: Cannot use a scalar value as an array and memory leak.
The script is very simple and I can not figure out the problem.
Code
$variants=array();
if($text)
{
$v=explode(",",$text);
if(is_array($v) && sizeof($v)>0)
{
foreach($v as $i=>$part)
{
$tmp=explode(":",$part);
list($thekey,$thevalue)=$tmp;
//$variants=array();
echo "<div>TYPE==".gettype($variants)."</div>";
echo $variants[$tmp[0]]=$tmp[1];
}
}
}
If I run the code above as stand alone is working fine. But when put it in my framework as small part behave very strange. I got a Warning: Cannot use a scalar value as an array and in order to solve it I added
$variants=array();
on the first line. When running the script the gettype returns ��� the first time and after that return integer.
If i uncomment $variants=array(); just before the gettype, it works. But of course I don't to get the whole array, only the last record return.
I parse my code to find out that the variables I use are declared before I even change all the variable names to stupids but no luck.
Trying to debug and tune the code where times that when running the script instead of see something in the screen the browser download the script instead and some other times I had memory leaks.
Can anyone point where or what to look for, or debug it and solve it?
Problem solved
Before run the code i was calling a function
$obj->draw($$id)
That was causing the problem
The solution
$value=$$id;
$obj->draw($value)
I dont know why this causing the problem.
If anyone has a theory please post it.

Categories