-2
\$\begingroup\$

I would like to know if there are any benefits of one of the following 2 methods which insert an object into a database

The first function calls the Model method statically, creating a new instance of the Insert function on the call.

foreach ($model_data as $data) {
    $row = \Models\ModelData::createRow($data, $u->dname, $w_reason, $comment);
    $insert = \Models\ModelData::Insert($row);
    $insert_obj = new \stdClass();
    $insert_obj->d_name = $data->{'Data Name'};
    $insert_obj->status = $insert;
    $insert_array[] = $insert_obj;
}

This version of the function calls the Model method non-statically.

foreach ($model_data as $data) {
    $row = \Models\ModelData::createRow($data, $u->dname, $w_reason, $comment);
    $insert_result = $row->Insert();
    $insert_obj = new \stdClass(); 
    $insert_obj->d_name = $data->{'Data Name'};
    $insert_obj->status = $insert_result;
    $insert_array[] = $insert_obj;
}

With the Model method being called non-statically, there is no need for an argument to be passed in.

public function Insert(): bool

But with the Model being created via a static method, it requires the argument.

public static function Insert(MyObject $object): bool

Is there a major benefit to one of these over the other?

\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$
  1. I think, in "Object-oriented programming", it makes perfect sense to explicitly craft your scripts to handle objects. The ability to chain methods together can help to make your code less verbose.
  2. $row and $insert_result are single-use variables, so I would not bother declaring it.
  3. Rather than forming an individual object, then declaring the properties, then pushing the object into the result array, I recommend the more brief syntax of populating the object inside of the push syntax.

Suggested, untested code:

foreach ($model_data as $data) {
    $insert_array[] = (object) [
        'd_name' => $data->{'Data Name'},
        'status' => (\Models\ModelData::createRow($data, $u->dname, $w_reason, $comment))->Insert()
    ];
}

While the above snippet doesn't quite fit neatly in the Stack Exchange text area, it does comply with the recommended line width limits described by PSR-12.

Alternatively, if you prefer to declare $row to lessen the line width, then you can use:

foreach ($model_data as $data) {
    $row = \Models\ModelData::createRow($data, $u->dname, $w_reason, $comment);
    $insert_array[] = (object) [
        'd_name' => $data->{'Data Name'},
        'status' => $row->Insert()
    ];
}

Ultimately, unless you have a valid reason to have Insert as a static method, I'd not recommend it. By the way, methods should start with a lowercase letter for PSR-12 compliance.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for the info! I'd like to follow up with a question to your statement "Ultimately, unless you have a valid reason to have Insert as a static method". What would a valid reason be for making Insert a static method? \$\endgroup\$ Commented May 6, 2022 at 11:20
  • \$\begingroup\$ Like "you need to have a shortcut path to Insert() without going through the class constructor and declaring the object". As a general rule, I don't use static methods unless it is valuable to circumvent non-static methods. Typically you use OOP, so that you can manipulate objects. I find static methods break up the flow that OOP affords. \$\endgroup\$ Commented May 6, 2022 at 11:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.