Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to handle erroneous argument type in this situation?
    text
    copied!<p>I'd like to use stdClass to store options for some methods, instead of passing huge lists of variables (inspired by javascript-style coding) </p> <p>However, I'd like to make sure I'm always getting an instance of stdClass as an argument. I know I can add a hint in the argument (gb::search below) but when I deliberately try to break it, I'm not sure how to handle the error.</p> <p>Any tips?</p> <pre><code>class gb extends CI_Model { protected $searchtypes = array('full','partial'); protected $endpoint = "https://local.endpoint"; function __construct() { parent::__construct(); // sample search $options = new stdClass(); $options-&gt;term = 'sample search'; $options-&gt;type = 'full'; $this-&gt;search($options); } function clean_term($term){ $term = trim($term); return $term; } function search(stdClass $options){ $term = $options-&gt;term; $type = $options-&gt;type; // make sure we're doing a valid search if (!$term || !in_array($type, $this-&gt;searchtypes)) { return false; } $term = $this-&gt;clean_term($term); // etc } </code></pre> <p>The error it throws is something like:</p> <pre><code>A PHP Error was encountered Severity: 4096 Message: Argument 1 passed to gb::search() must be an instance of stdClass, null given, called in /application/models/gb.php on line 20 and defined Filename: models/gb.php Line Number: 29 A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: models/gb.php Line Number: 31 A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: models/gb.php Line Number: 32 </code></pre> <p>Any ideas how to approach this from a CodeIgniter point of view?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload