Home > database >  How to update an object (of stdClass) in a couple of static defined functions?
How to update an object (of stdClass) in a couple of static defined functions?

Time:10-30

As follow-up of my question of yesterday (Is it possible to update an object defined in a public function from another public function inside a PHP class?), I have a new problem.

Actually, my problem is with updating objects (of stdClass) inside static functions.

I am working on a website where the new PayPal SDK V2 will be used for receiving payments. In the the script CaptureOrder class PayPal uses a static function (by default). The class is called like: CaptureOrder::capture_order($orderId, FALSE);

To validate response in different situations I created a few more functions which I also defined as static, in the same class.

I have to use at least 2 objects (stdClass) and fill them with validated or failed responses; one is for returning to the client (json) and one is for handling errors occured.

In an online PHP-editor I tested with some examples like below, but it keeps failing to update an object from another static function.

I have tried the below examples, but with no success:

class test {
  public static function setObj() {
    $obj1 = (object)[];
    $obj1->item1 = 'value1';
    echo '(line 5, obj1) : ', var_dump($obj1), "\n";
  }

  public static function objUpdate() {
    echo '(objUpdate) obj1: ', var_dump($obj1), "\n";
    echo '(objUpdate) self obj1: ', var_dump(self::setObj()->$obj1), "\n";

    self::setObj()->$obj1->item2 = 'varlue2'; // Warning: Undefined variable $obj1   Fatal error ...
  }
}

(line5, obj1) : object(stdClass)#2 (1) { ["item1"]=> string(6) "value1" }

(objUpdate) obj1: - Warning: Undefined variable $obj1 - NULL

(objUpdate) self obj1: (obj1) : object(stdClass)#2 (1) { ["item1"]=> string(6) "value1" }

[another try]
  public static function objUpdate() {
    $newObj = self::setObj()->$obj1; // Warning: Undefined variable $obj1
    echo '(objUpdate) newObj: ', var_dump($newObj), "\n";
  }

Warning: Attempt to read property "" on null - (objUpdate) newObj: NULL

[another test]

class test {
  private static $obj1;
  private static $obj2;

  public static function setObj() {
    self::$obj1 = (object)[];
    self::$obj1->item1 = 'value1';
 echo '(line7, obj1) : ', var_dump(self::$obj1), "\n";
  }

  public static function objUpdate() {
    self::$obj1->item2 = 'value2'; // Fatal error: Uncaught Error: Attempt to assign property "item2" on null
echo '(objUpdate) obj1: ', var_dump(self::$obj1), "\n";
  }
}

(line7, obj1) : object(stdClass)#2 (1) { ["item1"]=> string(6) "value1" }

(objUpdate) obj1: NULL

I found out that in recent PHP version it is no longer possible to use something like private $obj1 = stdClass(); - I get the error that it is deprecated, same with public $obj1 ...

As you can see in the examples, in function setObj I can define and update the object, but then in function objUpdate $obj1 has become NULL (I have no idea why).

So my question is, what am I doing wrong? How can I update objects like $obj1 from 2 or 3 different static functions in the same class?

Thank you in advance.

CodePudding user response:

You need to actually declare obj1 object as a static property, then you will be able to set/get it in your static functions using static accessors.

Here is the correct code to do that -

class test {
  
  public static $obj1;

  public static function setObj() {
    self::$obj1 = new stdClass();
    self::$obj1->item1 = 'value1';
    echo '(line 5, obj1) : ', var_dump(self::$obj1), "\n";
  }

  public static function objUpdate() {
    echo '(objUpdate) obj1: ', var_dump(self::$obj1), "\n";
    self::setObj();
    echo '(objUpdate) self obj1: ', var_dump(self::$obj1), "\n";
    
    self::$obj1->item2 = 'value2'; 
  }
}
  •  Tags:  
  • php
  • Related