-1

Instead of having a getter and a setter for each column I want to update from the Model I would like to use __get and __set in my Model but it doesnt work.

here's an example:

class Video extends CI_Model {

  private $video_id = null;
  private $title;
  private $url;
  private $thumb;
  private $width;
  private $height;

 public function __set($name, $value){
    if(!$this->video_id) return false;
    if(property_exists($this, $name))
    {
       $data = array(
          $name      => trim($this->security->xss_clean($value)),
      'updated'  => date('Y-m-d H:i:s'),
      );
      if($this->db->update('users', $data, array('video_id' => (int)$this->video_id)))
      {
          return $this->db->affected_rows();
      }
      else
      {
      return $this->db->_error_message();
      }
    }
 }

}

then from my Controller I do:

$this->video->width(780);

which produces:

<b>Fatal error</b>:  Call to undefined method Video::width() in <b>/home/crazy/public_html/dev/application/controllers/admin.php</b> on line <b>169</b><br />
hakre
  • 193,403
  • 52
  • 435
  • 836
Stephane
  • 4,978
  • 9
  • 51
  • 86
  • you are missing that __call() would be for methods, __set() is for properties. So you just made a typo. Double check the manual first if you have the feeling something would not work as expected. – hakre Apr 28 '13 at 09:19
  • not sure what you mean, can you elaborate a bit more please ? – Stephane Apr 28 '13 at 09:21
  • Well, let's turn it the other way round: Why do you expect that your code should *not* give that error? Please elaborate. – hakre Apr 28 '13 at 09:26
  • Also are you aware that we have this error message generally documented in the [PHP error reference](http://stackoverflow.com/q/12769982/367456)? This might be a helpful resource for you, so I leave you the link here: http://stackoverflow.com/q/12769982/367456 – hakre Apr 28 '13 at 09:29
  • Well, because to my understanding when I make $MyObject_instance->age(10); and the age property is not accessible (like a private attribute in the class) PHP call the __set() method if available, so in $this->video->width(780); I'm expecting __set() to be called with parameters $name = 'width' and $value = '780' you see what I mean ? – Stephane Apr 28 '13 at 09:30
  • weird some people vote to close the question instead at least replying... – Stephane Apr 28 '13 at 09:44
  • so you think that a property can be read/set by calling a method that has the same name? Are you aware that there is a difference between a *Property* and a *Method* in PHP? – hakre Apr 28 '13 at 09:47
  • And as I already wrote: If your understanding is not what happens, check the PHP manual for the methods you make use of if you have chosen the right ones: http://php.net/__set – hakre Apr 28 '13 at 09:50

1 Answers1

2

First off, you need to carefully read the documentation on this stuff. You are using the wrong magic method. __set is used for setting object properties:

$obj->width = 780; // Will be translated to:
$obj->__set('width', 780);

If you use __call, then a method will be overloaded:

$obj->width(780); // Will be translated to:
$obj->__call('width', array(780));

Second, you need to take a look at the CI source code for CI_Model. If I linked to the correct version then it sets its own __get method, which you will have to account for if you want to set your own getter.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • Thank you! well I just forget the correct way is to use an assignment not a call as I'm setting a property value! – Stephane Apr 28 '13 at 11:22