Home > Backend Development > PHP Tutorial > 关于类的$this的有关问题

关于类的$this的有关问题

WBOY
Release: 2016-06-13 13:10:16
Original
875 people have browsed it

关于类的$this的问题

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
class User {
   private $uid;        
   private $fields;    

   public function __construct() {
            $this->uid = null;
            $this->fields = array(
                            'username' => '',
                            'password' => '',
                            'emailAddr' => '',
                            'isActive' => false,
                            'permission' => 0
                         );
   }

   public static function getById($user_id) {
            $user = new User();
            
            $query = sprintf('SELECT username, password, email_addr, is_active, permission FROM %suser WHERE user_id = %d',
                                DB_TBL_PREFIX, $user_id);
            $result = mysql_query($query, $GLOBALS['DB']);
            if (mysql_num_rows($result)) {
                $row = mysql_fetch_assoc($result);
                
                $user->username = $row['username'];
                $user->password = $row['password'];
                $user->emailAddr = $row['email_addr'];
                $user->isActive = $row['is_active'];
                $user->permission = $row['permission'];
                $user->uid = $user_id;
            }
            mysql_free_result($result);
            
            return $user;
        }
   }

   public function save() {
            if ($this->uid) {
                $query = sprintf('UPDATE %suser SET username = "%s", password = "%s", email_addr = "%s", 
                                        is_active = %d, permission = %d WHERE user_id = %d',
                                                        DB_TBL_PREFIX,
                                                        mysql_real_escape_string($this->username, $GLOBALS['DB']),
                                                        mysql_real_escape_string($this->password, $GLOBALS['DB']),
                                                        mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
                                                        $this->isActive,
                                                        $this->permission, 
                                                        $this->uid);
                return mysql_query($query, $GLOBALS['DB']);
            }
            else {
                $query = sprintf('INSERT INTO %suser(username, password, email_addr, is_active, permission) 
                                                                                VALUES("%s", "%s", "%s", %d, %d)',
                                                        DB_TBL_PREFIX,
                                                        mysql_real_escape_string($this->username, $GLOBALS['DB']),
                                                        mysql_real_escape_string($this->password, $GLOBALS['DB']),
                                                        mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
                                                        $this->isActive,
                                                        $this->permission);
                if (mysql_query($query, $GLOBALS['DB'])) {
                    $this->uid = mysql_insert_id($GLOBALS['DB']);
                    return true;
                }
                else
                    return false;
            }
        }

Copy after login



为什么save()中的
$query = sprintf('UPDATE %suser SET username = "%s", password = "%s", email_addr = "%s", 
is_active = %d, permission = %d WHERE user_id = %d',
DB_TBL_PREFIX,
mysql_real_escape_string($this->username, $GLOBALS['DB']),
mysql_real_escape_string($this->password, $GLOBALS['DB']),
mysql_real_escape_string($this->emailAddr, $GLOBALS['DB']),
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template