2

I'd like to use stdClass to store options for some methods, instead of passing huge lists of variables (inspired by javascript-style coding)

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.

Any tips?

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->term = 'sample search';
    $options->type = 'full';

    $this->search($options);

}

function clean_term($term){
    $term = trim($term);
    return $term;
}

function search(stdClass $options){

    $term = $options->term;
    $type = $options->type;

    // make sure we're doing a valid search
    if (!$term || !in_array($type, $this->searchtypes)) {
        return false;
    }

    $term = $this->clean_term($term); // etc




}

The error it throws is something like:

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

Any ideas how to approach this from a CodeIgniter point of view?

tereško
  • 58,060
  • 25
  • 98
  • 150
danp
  • 14,876
  • 6
  • 42
  • 48
  • 1
    Try this: http://stackoverflow.com/a/2468534 – grossvogel Dec 02 '11 at 18:49
  • You could use `instanceof` to check that it's an instance of stdClass and explicitly reject/fail it if it isn't? – Mr. Llama Dec 02 '11 at 18:59
  • I could, but the error is triggered on entering the method, not inside it (I think), so this isn't really what I'm after. The construction seems to be missing something and logic inside the method doesn't seem right. Else, why would you want to specify the type in the argument list...? – danp Dec 02 '11 at 22:25

1 Answers1

1

if I remember - mistyped argument should raise E_RECOVERABLE_ERROR, so, it triggers error handler but execution continues. So, you have two options basically.

One is to throw exception in error handler when E_RECOVERABLE_ERROR is encountered. To halt execution.

Another - check type with instanceof stdClass and do what you suppose - raise exception or return something.

UPDATE In your case your framework (CI is for CodeIgniter?) sets error handler (somewhere using set_error_handler). So, after logging or printing error message execution continues. (If there was not handler you would get fatal error). Just test type of argument manually:

function search(stdClass $options){
  // test type for sure, because of recoverable error
  if (!($options instanceof stdClass)) {
    return false; // or throw new InvalidArgumentException('Parameter should be instance of stdClass');
  }

  $term = $options->term;
  $type = $options->type;

  // make sure we're doing a valid search
  if (!$term || !in_array($type, $this->searchtypes)) {
    return false;
  }

  $term = $this->clean_term($term); // etc
}
dmitry
  • 4,989
  • 5
  • 48
  • 72