1

I have seen this question being asked many times, but I am yet confused and came across something similar condition. I am not sure if this and my context are same or not, as long as I am convinced this is a different scenario or else I might have misunderstood the explanation. O.k. here is my scenario:

$amount = isset($cost->getCostAmount()) ? $cost->getCostAmount() : 0;

Function costAmount() is dynamically added during run-time and it may or may not exist. So I need to first check if my function exists or not and rest is pretty clear. But now in this case I get a fatal error:

Fatal error: Can't use method return value in write context in ..../file.php

Now if I do something like this:

$amount = $cost->getCostAmount() ? $cost->getCostAmount() : 0;

Obviously I would get an error:

Call to undefined method: getCostAmount

if the function doesn't exist. What could be a possible solution for this? Explanation will be considered helpful.

Request: Please add an adequate comment to why the question has been downvoted so that I would be able to improve my questions, in the future.

Community
  • 1
  • 1
ro ko
  • 2,906
  • 3
  • 37
  • 58

4 Answers4

8

change this:

$amount = isset($cost->getCostAmount()) ? $cost->getCostAmount() : 0;

to..

$amount = method_exists($cost, 'getCostAmount') ? $cost->getCostAmount() : 0;

because this piece of code isset($cost->getCostAmount()) is executing method getCostAmount even if it doesn't exist

Peter
  • 16,453
  • 8
  • 51
  • 77
4

isset requires you pass a variable to it not a function. It can not check whether the return value is set.

You should use it like this,

$cost_amount = $cost->getCostAmount();
$amount = isset($cost_amount) ? $cost_amount : 0;

Even this code does not make sense. Because here $cost_amount will be always set. If getCostAmount returns null or empty string you should check it that way.

$cost_amount = $cost->getCostAmount();
$amount = !is_null($cost_amount) ? $cost_amount : 0;

Also your code does not find getCostAmount function. If you know this is declared somewhere include it. If this method is generated dynamically you can check by using method_exists.

 $amount = method_exists($cost, 'getCostAmount')? $cost->getCostAmount(): 0;
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Thanks for the "isset requires you pass a variable to it not a function" but seems like you didn't get my context here, this would also return that the function doesn't exist. – ro ko Aug 26 '12 at 11:49
  • yes now it would work ofcourse and I am very thankful as well but I donot know if you were in too much hurry but this came after I don't even know how many edits :) And no it wasn't that I hadn't tried it properly because when I last read it you only had two lines of code and that didn't make too much sense, infact those things were already mentioned in the question. Yet +1 for the explanations :) – ro ko Aug 26 '12 at 12:01
1

Try changing isset() to function_exists()

tomsv
  • 7,207
  • 6
  • 55
  • 88
  • If that's the only change, the method invocation will still be present and the error message will stay exactly the same; besides, `function_exists()` will not work as you expect when used on a _method_ and not a plain function. – lanzz Aug 26 '12 at 11:39
0

You can use: method_exists (http://www.php.net/manual/en/function.method-exists.php) in the following way:

var_dump(method_exists($cost,'getCostAmount'));
Romain
  • 6,322
  • 3
  • 35
  • 40