0

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.

3
  • For some performance tipps. If you fetch something and build an array you put all your database entries in the memory. That can cause issues if you have a lot of entries and the memory limit is not high enough. Better you work directly with the output. Commented Jan 10, 2017 at 20:12
  • Thank you, this table only has about 30 or so rows and won't grow. Useful to know! Commented Jan 10, 2017 at 20:19
  • Then its not a big problem but if you have many thousand entries and they grow its possible that you apple run one day over your memory limit and you site is offline. But with 30 entries is not a problem. Commented Jan 10, 2017 at 20:43

1 Answer 1

1

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.

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

7 Comments

Oh I see! So that explains why it wouldn't work the same. Thank you, that makes things significantly more clear! Is my present method the most efficient way to do so?
It looks ok, you can also just avoid the entire loop and return $query->result(); Maybe I don't understand your loop fully but it seems redundant.
I'm trying to query the database for specific columns to feed a template, basically. Thanks for the $query->result() tip. As I get my feet wet further I will try to optimize this code once I get a better understanding of what I'm doing.
Last quick tip, it's better to avoid using SELECT *, pick only select values when possible, ,ie SELECT id, name...
Even if I will use all of the records? Why is that?
|

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.