2014-02-26 27 views
-2

任何人都可以幫助我與PHP類的例子。我必須提供有關用戶信息的課程「信息」:身份證,電子郵件,密碼,名字,姓氏,電話。PHP類的例子

此外,類必須有一個方法來打印輸出上的所有用戶數據。

+4

首先嚐試的東西嗎? –

+1

閱讀,閱讀,閱讀:http://fr2.php.net/language.oop5.php – Brice

+0

http://www.amazon.co.uk/Objects-Patterns-Practice-Matt-Zandstra/dp/1430260319/ref = sr_1_1?s = books&ie = UTF8&qid = 1393410657&sr = 1-1&keywords = php + objects + patterns + and + practice:我保存一份平裝本 – CD001

回答

0

這裏是一個PHP類的例子:

class DBIGenerator{ 
    private $table; 
    private $name; 
    private $path; 
    public function __construct($table,$name='default_file.php', 
      $path='DEFAULTPATH/'){ 
     $this->table=$table; 
     $this->name=$name; 
     $this->path=$path; 
    } 
    public function generate(){ 
     // build class header 
     $str='<?php class '.$this->name.'{'; 
     if(!$result=mysql_query('SHOW COLUMNS FROM '.$this->table)){ 
      throw new Exception('Failed to run query'); 
     } 
     // build data member declaration 
     if(mysql_num_rows($result)<1){ 
      throw new Exception('Not available columns in table'); 
     } 
     $methods=''; 
     while($row=mysql_fetch_array($result,MYSQL_ASSOC)){ 
      $str.='private $'.$row['Field'].'=\'\';'; 
      $methods.='public function set'.$row['Field'].'($'.$row 
       ['Field'].'){$this->'.$row['Field'].'=$'.$row 
       ['Field'].';}'; 
      $methods.='public function get'.$row['Field'].'(){return 
       $this->'.$row['Field'].';}'; 
      // store field names in array 
      $fields[]=$row['Field']; 
     } 
     // build empty constructor 
     $str.='public function __construct(){}'; 
     // build modifiers and accessors 
     $str.=$methods; 
     // build load() method 
     $str.='public function load(){$r=mysql_query("SELECT * FROM 
       '.$this->table.' WHERE id=\'$this->id\'");'; 
     $str.='return mysql_fetch_array($r,MYSQL_ASSOC);}'; 
     // build submit() method 
     $str.='public function submit(){mysql_query("INSERT INTO '.$this- 
       >table.' SET '; 
     foreach($fields as $field){ 
      $str.=($field!='id')?$field.'=\'$this->'.$field.'\',':''; 
     } 
     $str.='");$this->id=mysql_insert_id();'; 
     $str=preg_replace("/,\"/","\"",$str).'}'; 
     // build update() method 
     $str.='public function update(){mysql_query("UPDATE '.$this- 
       >table.' SET '; 
     foreach($fields as $field){ 
      $str.=($field!='id')?$field.'=\'$this->'.$field.'\',':''; 
     } 
     $str=preg_replace("/,$/","",$str); 
     $str.=' WHERE id=\'$this->id\'");}'; 
     // build delete() method 
     $str.='public function delete(){mysql_query("DELETE FROM '. 
       $this->table.' WHERE id=\'$this->id\'");}'; 
     $str.='}?>'; 
     // open or create class file 
     if(!$fp=fopen($this->path.$this->name.'.php','w')){ 
      throw new Exception('Failed to create class file'); 
     } 
     // lock class file 
     if(!flock($fp,LOCK_EX)){ 
      throw new Exception('Unable to lock class file'); 
     } 
     // write class code to file 
     if(!fwrite($fp,$str)){ 
      throw new Exception('Error writing to class file'); 
     } 
     flock($fp,LOCK_UN); 
     fclose($fp); 
     // delete temporary variables 
     unset($fp,$str,$row,$fields,$field,$methods); 
    } 
    public function getObject(){ 
     // check if class file exists 
     if(!file_exists($this->path.$this->name.'.php')){ 
      throw new Exception('Failed to include class file'); 
     } 
     require_once($this->path.$this->name.'.php'); 
     // create data access object 
     return new $this->name; 
    } 
} 

更多的http://www.devshed.com/c/a/PHP/Building-ObjectOriented-Database-Interfaces-in-PHP-Updating-the-Application-to-PHP-5/1/#Czocu1kMhhuTvg2e.99

2

這是很簡單的骨架,因爲你沒有嘗試任何事情,所以才讓你有想法如何工程...

class User 
{ 
    private $id; 
    private $email; 
    // ... 

    public function __construct($id, $email...) 
    { 
     $this->id = $id; 
     $this->email = $email; 
     // ... 
    } 

    public function printAll() 
    { 
     return $this->id . ' ' . $this->email; 
    } 
} 
0
?php 
class information 
{ 
public $id = 1; 
public $email = "[email protected]"; 
public $pw = "A2D7DFEA88AC88"; //Don't forget, PW are usually hashed ;) 
public function id() { 
echo $this->id; 
} 
public function email() { 
echo $this->email; 
} 
public function pw() { 
echo $this->pw; 
} 
} 
$test=new information(); 
$test->id; 
?>