Reputation: 923
I've got this line in code in module CMap.php which is part of the yii framework which should count the number of elements in array _d
.
return count($this->_d);
Howver, if _d
is an array[0]
it raises the error:
Trying to obtain property of non object
Using PHP v5.3.3 (x86) which is the same version as installed on our webserver.
Any idea why this is happening? Thanks.
EDIT: Changed the code to this:
try {
return null;
if ($this->_d==null)
return null;
else {
if(isset($this->_d[$key]))
return $this->_d[$key];
else
return null;
}
}
catch (Exception $e) {
return null;
}
And it still throws an errors on the first return null;
line without entering the catch()
block.
Upvotes: 1
Views: 168
Reputation: 923
Ok, I'm going to close this thread off now.
The code posted in my OP wasn't buggy, and wasn't causing the error.
Complete red herring. What is happening is that the code running after this method (the code which calls this method) is throwing the error, but for some reason that code doesn't show up in my debugger. I can't step through it.
The first I see of any debugging is when a breakpoint in the ErrorHandler gets hit.
I'll raise a separate Question on how/why the YII framework is doing that.
Thanks for you comments guys.
Upvotes: 0
Reputation: 98015
Let's break down the error message:
Trying to obtain property
A "property" is a field of some object, and "property access" is what you do with the ->
operator. In your case, $this->_d
.
of non object
In the expression $this->_d
, we would say "_d
is a property of $this
". So "of a non-object" means the thing on the left of the ->
operator is not an object.
So assuming the code you've posted is accurate, and not anonymized to the point where it's hiding the real error, your problem is that $this
is not an object in that part of the code.
Note that this contradicts your claim that $this->_d
is an empty array, because if $this
is not an object, then $this->_d
does not exist. So I strongly suspect that you're looking in the wrong place, or you've tried to simplify the description but missed out crucial details.
Upvotes: 3