0

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

ey dee ey em
  • 7,991
  • 14
  • 65
  • 121

1 Answers1

4

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);
kero
  • 10,647
  • 5
  • 41
  • 51
  • Would you tell me when I should use . to access? is . to access a variable instead of a function? – ey dee ey em Dec 04 '13 at 15:28
  • 1
    In OOP you always use `->` or `::` (for static attributes/methods) – kero Dec 04 '13 at 15:30
  • 1
    @Chen `.` in PHP is the *string concatenation operator*, nothing more. It has nothing to do with objects. – deceze Dec 04 '13 at 15:30
  • Thanks... I think I am a bit intertwined with my thinking in javascript. Thank for all the explanations! – ey dee ey em Dec 04 '13 at 15:31
  • @Chen You are welcome. I'm finding myself writing invalid JS because I concatenate with `.` instead of `+`.. If this answer helped you solve the problem, please consider marking it as accepted – kero Dec 04 '13 at 19:37
  • @kingkero Sure! Also, would you come this SO I made is one more step further for my problem. Wonder if you could give me some advise and help on that will really really appreciated! :)[It here](http://stackoverflow.com/questions/20383568/calling-a-key-of-a-json-decoded-array-return-null) – ey dee ey em Dec 05 '13 at 17:55