array to object throwing Uncaught Error: Call to undefined method stdClass

array to object throwing Uncaught Error: Call to undefined method stdClass



I am trying to reconstruct an object from code. First I explode the calls to the object into an array, and then convert the array into an object, and then test to see if the call works.



The object created seems fine, but the test call fails.


$claim = new_client();
print_r($claim);
$pat = $claim->patientFirstName();
echo $pat;

function new_client()

$text = '
$claim->patientFirstName()
UNWANTED STUFF.
$claim->patientMiddleName()
UNWANTED STUFF.....
$claim->patientLastName() ';


$client = array();
$i = 1 ;
$array = (explode("claim->",$text));

foreach ($array as &$variable)
$variable = substr($variable, 0, strpos($variable, "()"));

$client[$variable] = $i;
$i++;

unset ($variable);
$object = new stdClass();
foreach ($client as $key => $value)

$object->$key = $value;

return $object;



Here is the result.


stdClass Object ( => 1 [patientFirstName] => 2 [patientMiddleName] => 3
[patientLastName] => 4 )
Fatal error: Uncaught Error: Call to undefined method
stdClass::patientFirstName()...



So any ideas as to why
$pat = $claim->patientFirstName();
is failing?






there seems to be a quotes and period mismatch. PHP code within single quotes will not be evaluated

– RamRaider
Sep 7 '18 at 12:09






you are assigning a function to $claim variable and try to call other function using that variable (treating it as an class object.). new_client() is a function or class object?

– Exterminator
Sep 7 '18 at 12:15






The result seems to be a well formed stdClass object as can been seen in the print_r when the code is run, so whilst this may be true, is it the answer?

– user2818170
Sep 7 '18 at 12:27






new_client() is a function that returns a class object or so it appears.

– user2818170
Sep 7 '18 at 12:32






It's property but not method. $pat = $claim->patientFirstName; will work

– splash58
Sep 7 '18 at 13:02


$pat = $claim->patientFirstName;




1 Answer
1



You are accessing the element is a wrong way.


$claim->patientFirstName
$claim->patientMiddleName






Thank you. You are right in so far as it works. What would I need to change in order to be able to access it in the same way? The objective is to reconstruct the original object, not to change the code to access a different object.

– user2818170
Sep 7 '18 at 14:14






Put a different way, why can't I access the object I created using $claim->patientFirstName()? What would an object that is accessible in this way look like in a print_r($object) or would they look the same?

– user2818170
Sep 9 '18 at 15:39







you are accessing it as partientFirstName is a function

– Exterminator
Sep 10 '18 at 5:07






the correct way to do it as $object->key_name

– Exterminator
Sep 10 '18 at 5:07






Sorry to be slow though isn't that what I have done? Except the key_name is a variable as it is looped through its values? $object->$key = $value;

– user2818170
Sep 13 '18 at 10:44



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)