简体   繁体   中英

Add property and value in a object array

im quite new in php OOP, and im using laravel has my framework of choice. Im creating a object user and create the record, the only thing i have to do is to check if one of my input fields(not required) was set, if it was set than i have to add in in the user object, but how can i add this property (mobilephone) and value in a exiting object, am i doing it right?.

code:

$user = User::create([
    'email'              => $userDat['email'],
    'name'               => $userDat['name'],
    'surname'            => $userDat['surname'],
]);

if (isset($userDat['mobilephone'])) {
    $user->mobilephone = $userDat['mobilephone'];
}

There're a few ways you can do it.

Prepare the array in advance

$data = [
    'email'              => $userDat['email'],
    'name'               => $userDat['name'],
    'surname'            => $userDat['surname'],
];

if ($phone = array_get($userDat, 'mobilephone')) {
    $data['mobilephone'] = $phone;
}

User::create($data);

Or create the object first then save it

$user = new User([
    'email'              => $userDat['email'],
    'name'               => $userDat['name'],
    'surname'            => $userDat['surname'],
]);

if ($phone = array_get($userDat, 'mobilephone')) {
    $user->mobilephone = $phone;
}

$user->save();

As you are currently, but with an extra query (Ill advised)

$user = User::create([
    'email'              => $userDat['email'],
    'name'               => $userDat['name'],
    'surname'            => $userDat['surname'],
]);

if ($phone = array_get($userDat, 'mobilephone')) {
    $user->mobilephone = $phone;

    $user->save();
}

If you absolutely have to add the property to the object, I believe you could cast it as an array, add your property (as a new array key), then cast it back as an object. The only time you run into stdClass objects (I believe) is when you cast an array as an object or when you create a new stdClass object from scratch (and of course when you json_decode() something - silly me for forgetting!).

Instead of:

$foo = new StdClass();
$foo->bar = '1234';

You'd do:

$foo = array('bar' => '1234');
$foo = (object)$foo;

Or if you already had an existing stdClass object:

$foo = (array)$foo;
$foo['bar'] = '1234';
$foo = (object)$foo;

Also as a 1 liner:

$foo = (object) array_merge( (array)$foo, array( 'bar' => '1234' ) );

or maybe you could do it this way

$user->{'mobilephone'} = $userDat['mobilephone'];

how can i add this property (mobilephone) and value in a exiting object, am i doing it right?.

This strongly depends on which framework you are using. In general, you can add arbitrary properties to any object in PHP (which does not mean you always should ).

In your case, you are using the Eloquent framework that's shipped with Laravel, which actually depends on dynamically assigned object attributes and are using it absolutely correctly . This is documented in-depth in the official documentation . Note that you'll probably have to call $user->save() at some point to actually save your user object (thanks to Ben for the hint).

Note that alternatively, you could assign all properties of your User instance that way:

$user = new User();
$user->email   = $userDat['email'];
$user->name    = $userDat['name'];
$user->surname = $userDat['surname'];

if (isset($userDat['mobilephone'])) {
    $user->mobilephone = $userDat['mobilephone'];
}

$user->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM