开发者

Interpreting simple PHP code

To illustrate the concept of Destructor that a database is updated if values changes before the object is destroyed following code block is given in the book :

<?php
  class use {
  private $_properties;
  private $_changedProperties //Keeps a list of the properties that were altered
  private $_hDB;

  //_construct and __get omitted for brevity

  function __set($propertyName, $value) {
     if(!array_key_exists($propertyName, $this->_properties))
     throw new Exception('Invalid property value!');

   if(method_exists($this, 'set'. $propertyName)) {
   return call_user_func(
                       array($this, 'set', $propertyName), $value);
    }
    else {
    //If the value of the property really has changed
    //and it's not already in the changedProperties array,
    //add it.

    if($this->_properties[$propertyName] !=$value && !in_array($propertyName,        $this->_changedProperties)) {
      $this->_changedProperties[] = $propertyName;
     }

Rest of the code was unnecessary code and has been omitted. Please explain the code from the point:

        if(method_exists($this, 'set'. $propertyName)) {
   return call_user_func(
                       array($this, 'set', $propertyName), $value);
    }
    else {
    //If the value of the property really has changed
    //and it's not already in the changedProperties array,
    //add it.

    if($this->_properties[$propertyName] !=$value && !in_array($propertyName, $this->_changedProperties)) {
      $this->_changedProperties[] = $propertyName;
     }

Why am i asking this is that, I w开发者_如何转开发ant to verify my interpretation/understanding of the code.


Your commented notes seem correct... but this has little to do with the actual destructor, though i assume the destructor checks the changedProperties member and writes them if there are any before destruction. But thats not really pertinent to your question so i think youre causing confusion by mentioning it.


Roughly, this code checks to see if there is a setter (a method that sets a value on a property) for the property with name given by the argument $propertyName, and if no such function exists it adds that property to an field that contains an array called _changedProperties.

More precisely: suppose $propertyName contains a string "Foo"

if(method_exists($this, 'set'. $propertyName)) {

If this object has a method (sometimes called a function) with the name setFoo

    return call_user_func(array($this, 'set', $propertyName), $value);

Invoke the method setFoo with first argument $value and return its result; equivalent to calling return $this->setFoo($value);, but the text Foo is parameterized by $propertyName.

}else{

Basically, this object has no method called setFoo.

    if($this->_properties[$propertyName] != $value 
       && !in_array($propertyName, $this->_changedProperties)) {

If the value of this property (Foo) has a stored value different to the one I know now and it does not appear in the _changedProperties array

        $this->_changedProperties[] = $propertyName;

Add the name of this property to the list of changed properties. (here, add Foo to the _changedProperties array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜