Hola
Estoy mirando un poco como trabaja una aplicación ([URL="http://www.dasprids.de/blog/2010/10/20/modern-application-design-part-2"]Modern Application[/URL]) para entender un poco más algunas técnicas POO y me surgió una duda con respecto a la implementación de los métodos mágicos que comenté antes.
La base es esta:
Código PHP:
Ver original<?php
/**
* Abstract domain model
*/
abstract class App_Model_ModelAbstract
{
/**
* Allowed fields in this entity
*
* @var array
*/
protected $_fields = array();
/**
* List of field values
*
* @var array
*/
protected $_values = array();
/**
* Create an new instance of this domain model
*
* @param array $values
*/
public function __construct
(array $values) {
foreach ($values as $name => $value) {
$this->$name = $value;
}
}
/**
* Set a field
*
* @param string $name
* @param mixed $value
* @throws App_Model_OutOfBoundsException When field does not exist
* @return void
*/
public function __set($name, $value)
{
$this->{$method}($value);
} else if (in_array($name, $this->_fields
)) { $this->_values[$name] = $value;
} else {
throw new App_Model_OutOfBoundsException('Field with name ' . $name . ' does not exist');
}
}
/**
* Get a field
*
* @param string $name
* @throws App_Model_OutOfBoundsException When field does not exist
* @return mixed
*/
public function __get($name)
{
return $this->{$method}();
} else if (in_array($name, $this->_fields
)) { throw new App_Model_RuntimeException('Trying to accessing field ' . $name . ' which value was not set yet');
}
return $this->_values[$name];
} else {
throw new App_Model_OutOfBoundsException('Field with name ' . $name . ' does not exist');
}
}
}
http://site.svn.dasprids.de/trunk/application/library/App/Model/ModelAbstract.php
El constructor es el encargado de llamar a cada método a partir de lo que reciva en $values, pero va a llamar a todos los metodos ya sean set o get, entonces es solamente una diferencia semantica ? el objeto podría funcionar solo con get por ejemplo ?
Acá está una implementaciíon de la clase anterior
Código PHP:
Ver original<?php
/**
* Article domain model
*/
class Blog_Model_Article extends App_Model_ModelAbstract
{
/**
* @see App_Model_ModelAbstract::$_fields
* @var array
*/
protected $_fields = array( 'id',
'title',
'content',
'slug',
'datetime',
'comments',
'tags',
'permanentUrl',
'absolutePermanentUrl'
);
/**
* Set the title
*
* @param string $title
* @return void
*/
protected function _setTitle($title)
{
$this->_values['title'] = $title;
$this->_setSlug($title);
}
/**
* Set the slug, will automatically be normalized
*
* @param string $slug
* @return void
*/
protected function _setSlug($slug)
{
$slug = iconv('utf-8', 'ascii//translit', $slug);
$this->_values['slug'] = $slug;
}
/**
* Get the perment URL
*
* @return string
*/
protected function _getPermanentUrl()
{
if (!isset($this->_values
['permanentUrl'])) { $urlHelper = new Zend_View_Helper_Url();
$this->_values
['permanentUrl'] = $urlHelper->url(array( 'year' => $this->datetime->get('yyyy'),
'month' => $this->datetime->get('MM'),
'day' => $this->datetime->get('dd'),
'slug' => $this->slug
), 'blog-article');
}
return $this->_values['permanentUrl'];
}
/**
* Get the absolute permanent URL
*
* @return string
*/
protected function _getAbsolutePermanentUrl()
{
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$baseUri = $request->getScheme() . '://'
. $request->getHttpHost();
return $baseUri . $this->permanentUrl;
}
}
http://site.svn.dasprids.de/trunk/application/modules/blog/models/Article.php