I'm facing a problem that I can't answer.
I have an object that I can var_dump and see, with all his variables, but when I try to assign one of his variable I get "Trying to get property of non-object"..
Here's the code (it's from an api):
$teacher = $teachers->loadById($obj->userId); //gives an object teacher
$firstName = $teacher->firstName; // That return the error
If I do a var_dump like this
$teacher = $teachers->loadById($obj->userId);
var_dump($teacher);exit;
$firstName = $teacher->firstName;
I can see that it's an object with an attribute "firstName"
var_dump() result:
Does anyone experienced a situation like this?
I'm using PHP 5.5.12 on a local setup using wamp.
Edit: I noticed something else, this bug only occurs on my local setup. Everything works with my online test setup.. That makes it even more strange..
Related
I'm running PHP 8 on PHPStorm, and I'm trying to create a test for a function that returns an IP string. I'm using PHPUnit & PHPMock
I have the following code inside my test:
$UserIpAddress = $this->getFunctionMock(__NAMESPACE__, 'ip_request');
$UserIpAddress->expects($this->once())
->method('getRealIpAddr')
->willReturn('127.0.0.1');
My error is
Method name is already configured
Yet when I remove it, I get an error that it's expected to be called.
I have copied and pasted the function name, and it definitely is inside the correct class.
My namespaces are correct, and the actual page that uses the function works fine.
Any ideas?
Thanks
Fixed! After a ton of checking various options, the code that should go in the test file is
$local = new ip_request();
$this->assertEquals('127.0.0.1', $local->getRealIpAddr());
and inside the actual class file
$ip = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
I don't think the ?? '127.0.0.1' should be left in for production though.
My PHP app is accessing MTURK via the API and getting an Assignment object back that looks like this:
I'm trying to access the Answer field. So far I've tried:
$temp = $theResult->Answer;
$temp = $theResult->get('Answer');
...but they throw errors.
What is the correct way to access this field?
This works on my installation:
$answers = $theResult->getAnswer();
I have a really serious problem that I have not seen before.
On a website we are using opensource SQC eshop, PHP Version 5.3.3-7+squeeze15 and there is some kind of problem with variable memory I think.
SQC uses notORM and here the problem starts with fatal error "Call to function on non object notORMResult" .
So I dug deeper and found the constructor of NotORM that looks like this:
function __construct(PDO $connection, NotORM_Structure $structure = null,NotORM_Cache $cache = null) {
$this->connection = $connection;
if($_GET['test']){
var_dump($structure);
}
if (!isset($structure)) {
$structure = new NotORM_Structure_Convention;
}
if($_GET['test']){
var_dump($structure);
}
$this->structure = $structure;
if($_GET['test']){
var_dump($this->structure);
exit("1");
}
$this->cache = $cache;
}
And so the output is NULL because the constructor gets no structure param so we create an object. Second output is the object. Then we set the object to attribute and then the THIRD OUTPUT IS NULL
How is this even possible? The site was running for about year and half and no problems till yesterday. I didn't made yet any updates to php and this thing really freaks me out 'cause it's not a constant problem. It just happens sometimes after 2 hours, sometimes after 2 mins and I have really no idea why is this happening.
And btw ... this is just the start it happens across the whole script. Object attributes are set but when you want to read them they give you NULL. There is also second website running on the same server, same php same configuration without problem.
Thanks for any ideas :)
How can I simplify this structure:
$properties = $rs->$domains[0];
$dn = $properties[0]->distinguishedname;
Because PHP gives error on such construction:
$dn = $rs->$domains[0][0]->distinguishedname
This should work, or rather none of them should work.
To access a member of a object using the ->operator you don't need to use the $ twice, it should throw an error or warning of some kind as far as i know.
$properties = $rs->$domains[0];
$dn = $properties[0]->distinguishedname;
Should be:
$properties = $rs->domains[0];
$dn = $properties[0]->distinguishedname;
Likewise:
$dn = $rs->$domains[0][0]->distinguishedname;
Should be:
$dn = $rs->domains[0][0]->distinguishedname;
That's the only problem I can see with your code.
Only looking at it quick it seems as though your code should work as long as the data is actually there. Perhaps you try to fetch distinguishedname from a domain that has no properties?
What does the php error say? Do a *var_dump* on your data structure to see that it actually contains the data you expect that it does.
It took me a while to get soap configured in php. Now I'm just trying to learn about it. I'm using the web service here to learn:
http://www.webservicex.net/WCF/ServiceDetails.aspx?SID=19
It says the WSDL is here:
http://www.webservicex.net/stockquote.asmx?wsdl
and this is my code:
$client = new SoapClient('http://www.webservicex.net/stockquote.asmx?wsdl');
$result = $client->getQuote("AAPL");
echo $result;
I'm getting an error saying "Object of class stdClass could not be converted to string". Now I realize that $result is an object, and I'm wondering how do I access data that the server populated? I tried to understand the WSDL but I'm getting nowhere. Is it supposed to be something like:
$result->price
? (That doesn't work by the way...)
Ideas?
When curious about datatypes from the soapserver, use the SoapClient->__getTypes() & SoapClient->__getFunctions() methods
If you return is still not clear, var_dump() your return and examine it.