Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Easy PHP question


yomama360

Recommended Posts

Im sure this is easy for those who know... I just dont know PHP well enough.

 

I have a block of code I want to make into a function. It works fine when not a function, but throws up errors when as a function. I have narrowed the problem to this line inside the function:

$products_price = $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id']));

When I replace this line with something simple like $products_price = 3; it works. I think its something to do with the $currencies->display_price part.

 

heres the error:

Fatal error: Call to a member function on a non-object in... and gives the file location and some random line

 

That line in question is in an if statement if that helps.

 

Thanks in advance.

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself.

Therefore, all progress depends upon the unreasonable man."

-- George Bernard Shaw

Link to comment
Share on other sites

Based on the small code fragment posted, one can only assume that $currencies has not been initialised with a valid object, particularly one that has the display_price() method defined.

 

You should look prior to that code fragment and ensure $currencies is being set correctly. Note that PHP is weakly typed, so $currencies can hold anything from an object to a scalar variable.

 

Hope this gives you a lead. :)

Link to comment
Share on other sites

Great lead Bennstein. Thanks. Here's the deal.. it looks like putting an object within a function is not all good with PHP. (but it worked fine outside the function).

 

so this is not good:

function myFunk ($value) {
  Some code...
  $products_price = $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id']));
  ...more code
}

because the $currencies is an object.

 

BUT you can pass it into the function like so:

function myFunk ($currencies, $value) {
  Some code...
  $products_price = $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id']));
  ...more code
}

Then call it like this:

$myVar = myFunk ($currencies, 51);

So thanks again and I hope this helps someone else in the future.

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself.

Therefore, all progress depends upon the unreasonable man."

-- George Bernard Shaw

Link to comment
Share on other sites

Ahh yep, looking at that code, $currencies would only be recognised within the function if either it is passed in as an argument (as you've shown) or if it has been declared as a global. Of course, it would also be recognised within the local scope if it was declared locally, but that obviously wasn't the intent here. :)

 

That is pretty stardard behaviour and certainly helps explain your error in the original post.

 

Glad you worked it out anyway. All the best. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...