2012-02-13 4 views
4

このトピックの基礎として、ZFクイックスタートcreate modelを使用しています。
この文脈で__constructとsetOptions()メソッドが何をしているのかを正確に理解したいと思います。
私が何回何度叩いても、私はこれらの2つの方法が何をしているのか分かりません。Zend Framework Quickstart Modelコンストラクタ

public function __construct(array $options = null) 
    { 
     //if it is an array of options the call setOptions and apply those options 
     //so what? What Options 
     if (is_array($options)) { 
      $this->setOptions($options); 
     } 
    } 

public function setOptions(array $options) 
    { 
     //I can see this starts by getting all the class methods and return array() 
     $methods = get_class_methods($this); 
     //loop through the options and assign them to $method as setters? 
     foreach ($options as $key => $value) { 
      $method = 'set' . ucfirst($key); 
      if (in_array($method, $methods)) { 
       $this->$method($value); 
      } 
     } 
     return $this; 
    } 

私は本当に(setOptonsに迷子に)、私はそれを達成しようとしているかを把握することはできません。私はそれがいくつかの行動を抽象化していることを理解しています、私はちょうど何を推測することはできません。
私が言うことができる限り、これはちょうどそんなに「なんで!」。私はそれが重要となるかもしれないので、それを理解したいと思います。

答えて

4

あなたは、配列

{ ["name"] => "RockyFord" } 

として$optionsを渡すとsetNameメソッドは、このクラスに存在するならば、setOptions方法は

setName("RockyFord"); 

を呼び出します。

foreach ($options as $key => $value) { // Loops through all options with Key,Value 
     $method = 'set' . ucfirst($key); // $method becomes 'setName' if key is 'name' 
     if (in_array($method, $methods)) { // Check if this method (setName) exists in this class 
      $this->$method($value); // Calls the method with the argument 
     } 
    } 
+0

ありがとう、私は突然すべてを理解しています。これにより、get *またはset *を明示的に呼び出す代わりに配列を渡すことができます – RockyFord