Model of work, getters, setters and contracts (PHP)

I am not sure if the title is the best way to describe this question.

This book - http://apress.com/book/view/9781590599099 - illustrates the implementation of the Unit of Work template. It is a bit like this.

class UoW(){
   private array $dirty;
   private array $clean;
   private array $new;
   private array $delete;

   public static function markClean(Entity_Class $Object){
   //remove the class from any of the other arrays, add it to the clean array
   }

   public static function markDirty(Entity_Class $Object){
   //remove the class from any of the other arrays, add it to the dirty array
   }

   public static function markNew(Entity_Class $Object){
   //add blank entity classes to the new array
   }

   public static function markDelete(Entity_Class $Object){
   //remove the class reference from other arrays, mark for deletion
   }

   public function commit(){
   //perform all queued operations, move all objects into new array, remove any deleted objects
   }
}

class MyMapper(){
  public function setName($value){
     $this->name = $value;
     UoW::markDirty($this);//here where the problem is
   }
} 

(Ignoring static call problems and dependencies for a moment)

The author notes that this implementation requires the encoder to insert the appropriate UoW marking methods and that this selective execution of the template can lead to errors. Now, taking on the pros and cons of specific accessories, you could automate the UoW call as follows:

public function __set($key,$value){
   //check to see if this is valid property for this class
   //assuming the class has an array defining allowed properties 
   if(property_exists($this,$key)){
       $this->$key = $value;
       UoW::markDirty($this);

       /*
       * or alternatively use an observer relationship 
       * i.e. $this->notify();
       * assuming the UoW has been attached prior to this operation
       */
      }
   } 

So, my question is: how would you like to guarantee that the corresponding UoW method was called when setting the properties of the domain object?

+3
1

, , - "", PHPDoc, "" . - :

/**
 * @property string $prop
 */
class Foo extends UoW {
    protected $prop = "foo";

    public function __set($name, $value) {
        if (property_exists($this, $name)) {
            $this->$name = $value;
            $this->markDirty();
        }
    }
}

, IDE , Foo- > prop , , .

0

Source: https://habr.com/ru/post/1753140/


All Articles