-1

Hello everyone I have this class in php

<?php
namespace ExternalImporter\application\libs\pextractor\parser;

defined('\ABSPATH') || exit;
class Product {

    public $link;
    public $domain;
    public $title;
    public $description;
    public $price;
    public $currencyCode;
    public $image;
    public $oldPrice;
    public $manufacturer;
    public $inStock;
    public $availability;
    public $category;
    public $condition;
    public $ratingValue;
    public $reviewCount;
    public $features = array();
    public $images = array();
    public $reviews = array();
    public $paused;
    public $categoryPath = array();
    public $extra = array();

}

However when this is called with a null value it gives me a fatal error. I need that when this error occurs it returns the result: error25

I call the class like this in a function

public static function update(Product $newProduct, $product_id)
{
//code
}

In this example, $newProduct has the value of NULL, which is why it would generate an error in the function.

If I can return the error string error25 I could handle it.

Note: If the solution is to call the function, it is much better than editing the class itself.

My goal is to achieve something like this, but I'm not sure you can in PHP

public static function update(Product $newProduct ?: "error25", $product_id)
{
//code
}

Edit Possible solution

public static function update($newProduct, $product_id)
{
if (empty($newProduct)) {
# CALL Product $newProduct how to do it?
}
else {
$newProduct = NULL;
}
}
Club
  • 111
  • 1
  • 8
  • 3
    [`is_null()`](https://www.php.net/manual/en/function.is-null.php) ? – Cid Jul 22 '22 at 07:25
  • Hello, Can you do it in an example please? – Club Jul 22 '22 at 07:25
  • 1
    `if (is_null($yourVariable)) { /* do something */ }` – Cid Jul 22 '22 at 07:27
  • 2
    You are wanting a "guard clause" _inside_ the method signature? How about validating the data prior to calling the method (a method which clearly states in its signature what data types are required)? [Related](https://stackoverflow.com/q/20593983/2943403) – mickmackusa Jul 22 '22 at 07:28
  • @Cid It is not a solution since it would take me to edit many lines of code. I am looking for a solution when calling the function or building the class. – Club Jul 22 '22 at 07:30
  • @mickmackusa yes as 2nd option but the 1st option would be calling the function I put an example of what you want at the end of the question. – Club Jul 22 '22 at 07:30
  • 2
    Very related to your previous question. Have you abandoned [that question](https://stackoverflow.com/q/73075325/2943403)? – mickmackusa Jul 22 '22 at 07:52
  • Yes, a lot of issues as mentioned above. Also, why will `update` be called with a null product value? – nice_dev Jul 22 '22 at 08:11

1 Answers1

0

First of all your error occurs not because of your parameter $newProduct is null and not an instance of Product. It occurs because you have logic in your method definition. You can 't do that.

public static function update(Product $newProduct ?: "error25", $product_id)

This should never work. It 's a syntax error. It results in: PHP Parse error: syntax error, unexpected token "?", expecting ")". Please set your error reporting to the highest level, so that such basic errors are visible to you.

Checking against null or instance of a class

<?php
declare(strict_types=1);
namespace Marcel;

use Exception;
use stdClass;

class Foo
{
    public static function bar(?stdClass $yadda): void
    {
        if ($yadda === null) {
            throw new Exception('$yadda is not an instance of stdClass', 25);
        }
        // or ...
        if (!$yadda instanceof stdClass) {
            throw new Exception('$yadda is not an instance of stdClass', 25);
        }

        // everything okay at this point
        $yadda->someProperty = 'I am a valid stdClass instance';
    }
}

What happens here? Your static method takes a parameter that can either be an instance of stdClass or null. If $yadda is null an exception will be thrown with a message and a code.

Catching type errors

With PHP 8 you can catch type errors. That 's another solution to your problem.

<?php
declare(strict_types=1);
namespace Marcel;

use stdClass;
use TypeError;

class Test
{
    public static function foo(stdClass $yadda): void
    {
        // some logic here
    }
}

try {
    Test::foo(null);
} catch (TypeErrror $error) {
    var_dump($error->getMessage());
    // Marcel\Test::foo(): Argument #1 ($bar) must be of type stdClass, null given
}

You can catch type errors in a try/catch block. If $yadda is something different than an instance of stdClass a type error will be thrown. You can handle this error in a catch block. With this solution you don 't have to declare your parameters as null or an instance of some class. You can just declare the type hint of a specific class and catch a type error when calling the static method.

Marcel
  • 4,854
  • 1
  • 14
  • 24
  • I will study it however I want to ask if there is any way to do it by calling something like this from the function? `public static function update(Product $newProduct ?: "error25", $product_id) { //code }` – Club Jul 22 '22 at 07:34
  • 2
    No, there is absolutely no way to define logic in your method definition. This always results in a syntax error. – Marcel Jul 22 '22 at 07:35
  • Dear @Marcel another solution would be to call the function like this and check if it is null `public static function update($newProduct, $product_id) { //code }` However, how could I call `Product $newProduct` inside the function? – Club Jul 22 '22 at 07:37
  • Just use the null-operator in your parameter List. A definition like `?Product $product` says, that the given parameter `$product` can be null or an instance of Product. Check against null in your method logic. Just as shown above. – Marcel Jul 22 '22 at 07:39
  • Dear @Marcel I edited the question but I have no idea how to call Product $newProduct inside the function – Club Jul 22 '22 at 07:43
  • I try to understand but that makes no sense. You have defined a parameter list in your method definition. Just call `$newProduct` in your method body. It can be null or an instance of `Product`. If it 's null, just return your error code or throw an exeception. Easy as pie. All mentioned in the code example above. Try to understand it. – Marcel Jul 22 '22 at 07:45
  • I happen to be a beginner in php. :( My question is how to call the class from the function but I don't know how to call it inside the function – Club Jul 22 '22 at 07:58
  • Normally to call the class in the function I do this `public static function update(Product $newProduct, $product_id) { //code }` Now I try to call the class inside the function doing this and it doesn't work. `public static function update($newProduct, $product_id) { $myvar = Product $newProduct; }` – Club Jul 22 '22 at 08:00
  • If you want to make sure that your parameter is an instance of a certain class, just use [type hints](https://www.php.net/manual/de/language.types.declarations.php). I have edited my post and now also offered you a second possible solution to your problem. Please try to understand the solutions and implement them for yourself. It is very noticeable that you lack know-how in terms of PHP basics. Just try for yourself understanding how classes, methods, parameters, type hints and exceptions work. I have given you everything you need for this. – Marcel Jul 22 '22 at 08:52