简体   繁体   中英

Best practice OOP with MVC and PHP functions?

New to OOP so I am trying to figure out best practice. This code is based off an existing script I am adding to.

Most of the threads with this question tell the poster to code as so:

function ($arg1, $arg2){
 //some code
}

and call:

function($a1, $a2);

I have an OOP-based function (that works) but it doesn't quite look right and when I try to call it as the suggested method, I get:

Array to string conversion .... on line .. Array

Here's my (working) function that gathers the output:

    public function getMail($type, $id = 0) {
        $query = $this->db->query("SELECT * FROM km_mail WHERE id = '" . (int)$id . "' AND `type` = '" . $this->db->escape($type) . "'");

        foreach ($query->rows as $result) {
            $mail_data[$result['title']] = $result['value'];
        }

    return $mail_data;
}

This is the working (but ugly) part - this returns the database column requested (but looks wrong?):

$this->model_setting_mail->getMail('order')['update_link'];

When I try to request the column like so, the array to string conversion error occurs:

$this->model_setting_mail->getMail('order','update_link');

In my example, order = $type, update_link = $result['value'] and $id = 0 is default, unless an $id is passed.

The first example you show is a shorthand way of selecting an array element from value returned by a function.

$this->model_setting_mail->getMail('order')['update_link'];

Is the same as:

$result = $this->model_setting_mail->getMail('order');
print $result['update_link'];

Second example is passing two values to a function.

They are completely different.

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