Q
PHP類的例子
-2
A
回答
0
我建議你閱讀這個:http://codular.com/introducing-php-classes然後回來的任何問題。
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;
}
}
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;
?>
相關問題
- 1. 例子/ PHP中類型的XSS攻擊
- 2. NSComboBoxCell子類的例子?
- 3. 實例的子類php與主類的現有值
- 4. 簡單的php websocket例子
- 5. 子類實例| Stackoverflowexception
- 6. 單例類的幾個例子
- 7. 演員案例類簡單的例子
- 8. 使用Point類的例子?
- 9. 獲取子類的實例
- 10. 到JSON的PHP類實例
- 11. PHP類的鏈實例化
- 12. PHP:從子類
- 13. 父類和子類的實例數
- 14. 超類的子類實例化
- 15. PHP函數類的類/例參數
- 16. 從Ruby中的父類實例化子類的實例
- 17. 如何從超類的實例創建子類的實例?
- 18. PHP - 遞歸例子解釋
- 19. AS3的子類的引用子類沒有實例化?
- 20. 用於子類實例的Java淺拷貝超類實例
- 21. 從父類的實例實例化子類
- 22. Objective-C子類化:兩個子類實例似乎指向相同的實例
- 23. PHP超類和子類
- 24. PHP分類與子分類
- 25. 超類和子類在PHP
- 26. 什麼是PHP中的MVC的例子?
- 27. 獲取子類實例
- 28. 訪問子類實例
- 29. Python實例化子類
- 30. 子類升壓例外
首先嚐試的東西嗎? –
閱讀,閱讀,閱讀:http://fr2.php.net/language.oop5.php – Brice
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