note 99453 modified in language.oop5.magic by danbrown
| From: | danbrown@php.net | Date: | Wed, 18 Aug 2010 15:46:22 +0000 |
| Subject: | note 99453 modified in language.oop5.magic by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171805@lists.php.net to get a copy of this message | ||
Thought I would just explain something about __get and __set here.
Imgine you have a class like so:
<?php
class user_bo{
protected $attributes = Array();
public function __get($key){
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
}
public function __set($key, $value){
$this->attributes[$key] = $value;
}
/** Constructor **/
public function __construct(){}
}
?>
now imagine you add a function like so to this class:
<?php
public function update(){
global $database;
$fields = $database->connection->getTable("users");
$set = "";
foreach($fields as $field => $info){
if(isset($this->$field)){
if($this->$field == "now()"){
$set .= $field."=".mysql_real_escape_string($this->$field).", ";
}elseif($this->$field == null){
$set .= $field."=null, ";
}else{
$set .= $field."='".mysql_real_escape_string($this->$field)."',
";
}
}
}
$output_set = substr($set, 0, -2);
$query = "UPDATE users SET $output_set WHERE sequal_user_id =
'$this->sequal_user_id'";
mysql_query($query) or sqlerrorhandler("(".mysql_errno().") ".mysql_error(),
$query, $_SERVER['PHP_SELF'], __LINE__);
return true;
}
?>
The if(isset($this->$field)) will return false everytime. Instead of isset in loops like this you
should use ($this->$field). isset returns false even when the field is populated and in theory it
should never return false since in my __get:
<?php
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
?>
I return null if my variable don't exist.
--was--
Thought I would just explain something about __get and __set here.
Imgine you have a class like so:
class user_bo{
protected $attributes = Array();
public function __get($key){
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
}
public function __set($key, $value){
$this->attributes[$key] = $value;
}
/** Constructor **/
public function __construct(){}
}
now imagine you add a function like so to this class:
public function update(){
global $database;
$fields = $database->connection->getTable("users");
$set = "";
foreach($fields as $field => $info){
if(isset($this->$field)){
if($this->$field == "now()"){
$set .= $field."=".mysql_real_escape_string($this->$field).", ";
}elseif($this->$field == null){
$set .= $field."=null, ";
}else{
$set .= $field."='".mysql_real_escape_string($this->$field)."',
";
}
}
}
$output_set = substr($set, 0, -2);
$query = "UPDATE users SET $output_set WHERE sequal_user_id =
'$this->sequal_user_id'";
mysql_query($query) or sqlerrorhandler("(".mysql_errno().") ".mysql_error(),
$query, $_SERVER['PHP_SELF'], __LINE__);
return true;
}
The if(isset($this->$field)) will return false everytime. Instead of isset in loops like this you
should use ($this->$field). isset returns false even when the field is populated and in theory it
should never return false since in my __get:
return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
I return null if my variable don't exist.
http://php.net/manual/en/language.oop5.magic.php