note 73403 added to function.get-object-vars
| From: | thiago dot henrique dot mata at gmail dot com | Date: | Wed, 21 Feb 2007 19:23:14 +0000 |
| Subject: | note 73403 added to function.get-object-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123677@lists.php.net to get a copy of this message | ||
<?php
# How to make a function change the private attributes
# from some object without use serialize functions or
# lose the control of the changes.
/**
* Parent Class to allow the change of privates attributes
* Look the abstract function __setAttribute.
*
* @author Renan de Lima ( renandelima@gmail.com )
* @author Thiago Mata ( thiago.henrique.mata@gmail.com )
* @date 2007-02-21
*/
abstract class father
{
/**
* Receive the Aray and try to change the attribute value
*
* @param array $arrNewValues
*/
public function __fromDatabase( $arrNewValues )
{
$arrToSet = array_intersect_key( $arrNewValues, get_object_vars( $this ) );
foreach( $arrToSet as $strAttribute => $mixValue )
{
$this->__setAttribute( $strAttribute , $mixValue );
}
}
/**
* Required method to control the attributes of class
* @param string $strAttribute
* @param unknown $mixValue
*/
abstract protected function __setAttribute( $strAttribute, $mixValue );
}
/**
* Just a example of a child class using the functionality
*
* Note: if you don't wanna to allow the change of some attribute
* by this method you can just make more complex the __setAttribute function.
*
* @author Renan de Lima ( renandelima@gmail.com )
* @author Thiago Mata ( thiago.henrique.mata@gmail.com )
* @date 2007-02-21
*/
class son extends father
{
private $atr = 9;
/**
* This is the most simple implementation of the method.
* This way it's allowed to the parent class change any attribute
* @param string $strAttribute
* @param unknown $mixValue
*/
protected function __setAttribute( $strAttribute, $mixValue )
{
$this->{ $strAttribute } = $mixValue;
}
}
$objSon = new son();
$objSon->__fromDatabase( array( 'atr' => 55 ) );
var_dump( $objSon );
?>
----
Server IP: 200.185.109.13
Probable Submitter: 200.130.3.101 (proxied: 10.210.5.101)
----
Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php
Edit -- https://master.php.net/note/edit/73403
Del: integrated -- https://master.php.net/note/delete/73403/integrated
Del: useless -- https://master.php.net/note/delete/73403/useless
Del: bad code -- https://master.php.net/note/delete/73403/bad+code
Del: spam -- https://master.php.net/note/delete/73403/spam
Del: non-english -- https://master.php.net/note/delete/73403/non-english
Del: in docs -- https://master.php.net/note/delete/73403/in+docs
Del: other reasons-- https://master.php.net/note/delete/73403
Reject -- https://master.php.net/note/reject/73403
Search -- https://master.php.net/manage/user-notes.php