Greetings, there is PHP code, the problem is that when I call the GetInventory() method, GetInventory() pass the parameter, but I need to make sure that if the parameter is not passed, I assign the standard values ​​of $this->param , but this does not work for me.

  class steam{ private $Bots_ID = array(); private $Weapons = array(); private $param = array('id','name','quality','market_name'); private $SteamID = 0; private $Link = "http://steamcommunity.com/profiles/[steamid]/inventory/json/730/2"; public function __construct($bots_id = array()){ $this->Bots_ID = $bots_id; } public function GetInventory($param = $this->param){ $this->SteamID = 1234567890; $this->param = $param; $data = json_decode(file_get_contents( preg_replace("/\[steamid\]/i", $this->SteamID, $this->Link) ), true); for ($i = 0; $i < count($data['rgInventory']); $i++) { $element_name = array_shift($data['rgInventory']); $code_item = $element_name['classid']."_".$element_name['instanceid']; $this->Weapons[$i]['id'] = $element_name['id']; } return $this->Weapons; } } $steam = new steam(); $items = $steam->GetInventory(); print_r($items); ?> 
  • if (!$param) $param = $this->param :) - Vasily Barbashev

2 answers 2

Set the value of the $param variable to null by default and do a check. If $param is null use $this->param .

Example:

 ... public function GetInventory($param = null){ ... $this->param = isset($param) ? $param : $this->param; ... 

or

 ... public function GetInventory($param = null){ ... if( ! isset($param) ){ $this->param = $param; } ... 

In the version of php 7 and above, you can use the new syntax. Example:

 ... public function GetInventory($param = null){ ... $this->param = $param ?? $this->param; ... 
  • one
    But what about the new php chip, such as $param ?? $this->param $param ?? $this->param - Vasily Barbashev
  • @ Vasily Barbashev The question does not indicate the version of php. A new "trick" will not work in version below 7. - Alexander Andreev
  • I do not criticize, I just ask, it would be interesting to see an example of such an implementation in the nom syntax. Why not? Development on the site is not worth it :) - Vasily Barbashev
  • @ Vasily Barbashev In the case of ?? need to use null instead of false . So I adjusted both previous versions, so that it would be uniform, and added a new one. - Alexander Andreev
  • Thank you, I think you can just somehow assign when taking the parameter itself, but no. - Vladislav Siroshtan
 ... public function GetInventory($param = null){ if($param === null) { $param = $this->param; } ...