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.

6
  • It means there is no method getCostAmount(), what is $cost ? Commented Aug 26, 2012 at 11:37
  • function_exists? Commented Aug 26, 2012 at 11:38
  • @MihaiIorga $cost is a object or some other class. Commented Aug 26, 2012 at 11:42
  • @BurhanKhalid Yes I have already tried that, it doesn't work. Commented Aug 26, 2012 at 11:43
  • @roko $cost has to be an initialized class for that to work. Commented Aug 26, 2012 at 11:44

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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;

2 Comments

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.
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 :)
1

Try changing isset() to function_exists()

1 Comment

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.
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'));

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.