0

Ola!

So I got a $_POST looking like:

Array
(
    [name] => FooBar
    [sobject] => tbl_character
    [id] => 102
)

And a "SmartObject" like:

SmartObject Object
(
    [_settings] => Array
        (
            [table] => tbl_character
            [ignores] => Array
                (
                    [0] => leaderid
                    [1] => typeid
                    [2] => senderid
                    [3] => recieverid
                    [4] => imageid
                    [5] => fileid
                    [6] => professionid
                    [7] => id
                )

            [prefix] => tbl_
        )

    [id] => 102
    [worldid] => 
    [accountid] => 110
    [zoneid] => 
    [raceid] => 1
    [imageid] => 
    [name] => asd

    ... blabla more data
)

What I want to do is to loop through the $_POST and check if the keys match any public set property on my SmartObject like so:

foreach($_POST as $key => $value) {
  if(isset($object->{$key})) {
    $object->{$key} = $value;
   }
}

When a value exists (ex. for id) the isset triggers and returns true, but when a value doesn't the isset wont return true.

empty() checks if a value is set.

isset() should check if the "variable" or "property" is there, not necessarily set to anything, right?

I believe this code worked fine for me a year ago, but now the if-statement wont trigger on name. Am I doing something wrong? Has the fundamentals of php changed?

1 Answer 1

1

How about (if you are using PHP >= 5.1):

foreach($_POST as $key => $value) {
    if (property_exists($object, $key)) {
        $object->$key = $value;
    }
}

Here's the documentation: http://www.php.net/manual/en/function.property-exists.php

From the official documentation:

Note: As opposed with isset(), property_exists() returns TRUE even if the property has the value NULL.

Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm! Though I got to ask, I do use php 5.4, but did the isset()-usage on object properties work prior to 5.1? In that case I must've used 5.1 or lower last time I coded on the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.