Skip to content

If isset function variable paramenters php

I have this function

    private static function applayPrice($objProduct, Product $product, $applay_price, $applay_old_price)
    {}

The problem is that sometimes the variable Product $product doesn’t exist and this gives me an error.

I would like to do a kind of if isset and if it doesn’t exist just apply the $product variable

I’m new to programming I hope I don’t offend anyone with this question hehe To explain myself better, I would like it to look like this, any ideas?

    private static function applayPrice($objProduct, if( isset( Product $product ) ) {Product $product}else{$product}, $applay_price, $applay_old_price)
    {}

I know that this cannot be done but more or less the idea is that

Answer

You can try to do this:

    private static function applayPrice($objProduct, Product $product = NULL, $applay_price, $applay_old_price)
    {}

This simply means if the $product is not set, then it will be NULL.

Please check this link too.