var_dump for a function in a class return Fatal error: Call to undefined function

StackOverflow https://stackoverflow.com/questions/20379255

  •  29-08-2022
  •  | 
  •  

سؤال

I tried to manipulate var dump with a function inside a class, but failed. I received the error for the 2nd var dump. 1st dump was correctly shows a json result with the array. here is the error:

Fatal error: Call to undefined function updateValue() in ...

I do not understand where I got wrong in term of how instance works with function inside the class it created from. Aren't they suppose to able to execute the function inside the class since that is where it directly related with?

code:

class User {
private $dbh;
public function __construct($host,$user,$pass,$db)  {   ...};
public function getQs(){            ...};
public function updateValue($user){     ...};
}
$user = new stdClass;
$userParams = array('id' => 1, 'field' => 'questTitle', 'newvalue' => "Baaaaa");
$user = json_encode(array("user"=>$userParams));
var_dump($user);
$user = json_decode($user);
$userN=new User(...); 
$dump=$userN.updateValue($user);
var_dump($dump);

NOTE: the ... are there to hide sensitive information, they are actual code/variable that is valid in place.

Please help. Thanks

هل كانت مفيدة؟

المحلول

You need to use -> instead of . to call member functions.

$dump=$userN->updateValue($user);

. as deceze mentioned is the string concatenation operator. So this code

$dump = $userN.updateValue($user);

would be equal to

$dump = $userN;
$dump .= updateValue($user);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top