断断续续,写啊写,从最简单的留言本(HTML和PHP完全混得一塌糊涂)到现在,差不多已经有两三年了。
可是现在感觉越写越倒退了,快无从下手了。原先的积累了一些小代码,后来改了discuz和uchome之后就发现自己原先的那些积累的代码太不行了,准备写个新的,但是现在发现无法手下了。对于PHP,比如index.php,必定是数据库最新或者最热的内容的列表,是用类来调用好还是直接将获取代码写在index.php里面好。
不知道大家明白了我的意思没discuz的是直接将sql语句和获取代码直接写在该前台文件里面。比如viewthread.php index.php。
大家是怎么样做的?
真的对PHP无从下手了,有点恐怖啊,还要用这个来吃饭nie。如果用对象来弄的话,我想过了就是每个表对应一个对象,我是这样的,但是这样假如表非常多的话,80多个100多个的话,那得建上百个class.php文件了。用哪种方法好?不过还是觉得将获取数据库内容的代码和处理的代码直接写在前台文件好一些,但是如果其他前台文件要用这个的话,又必须再写一次,很不便管理。大家有什么好的方法和建议。
(写得有点乱,希望大家可以看得懂。)
可是现在感觉越写越倒退了,快无从下手了。原先的积累了一些小代码,后来改了discuz和uchome之后就发现自己原先的那些积累的代码太不行了,准备写个新的,但是现在发现无法手下了。对于PHP,比如index.php,必定是数据库最新或者最热的内容的列表,是用类来调用好还是直接将获取代码写在index.php里面好。
不知道大家明白了我的意思没discuz的是直接将sql语句和获取代码直接写在该前台文件里面。比如viewthread.php index.php。
大家是怎么样做的?
真的对PHP无从下手了,有点恐怖啊,还要用这个来吃饭nie。如果用对象来弄的话,我想过了就是每个表对应一个对象,我是这样的,但是这样假如表非常多的话,80多个100多个的话,那得建上百个class.php文件了。用哪种方法好?不过还是觉得将获取数据库内容的代码和处理的代码直接写在前台文件好一些,但是如果其他前台文件要用这个的话,又必须再写一次,很不便管理。大家有什么好的方法和建议。
(写得有点乱,希望大家可以看得懂。)
一个网站可以有通用的页头页脚, 其中可以架设不同的应用程序,通用的类库...建议楼主你先看CI,看完再看cakephp, 再整合一下他们的思想, 自己写一个轻便的框架, 论负载, 扩展绝不比discuz差...
倒是WordPress代码让我觉得我还是个菜鸟
看样样子你还没达到9段阶段,才达到5段。
谁知道php咋想别的语言一样转向 好像没有response.redirect
第二问题:如果你是封装model的,只封装被引用很多,且是业务逻辑的否则,只直定sql语句查询即可!因有的业罗辑比较复杂的页,可能要调用几十个查询,都写成类方法,个人认为没有必要!直接查询取数据就行了!否则就有点形而上学了!
直接用PHP来做有什么不好,非得左调右调的。但是去公司面试 一般都要求会。但是假如一个网站真的用了cakephp之类的框架的话,要对他进行维护,一般的phper改不了。感觉就是“将简单的问题复杂化”,“形而上学”很多地方根本用不到框架或者smarty 。
尤其是做二次开发的,而PHP我感觉大多数都是做二次开发的,还是要看的懂,知道从哪下手改才行。
哪怕是不遵守它原来的设计思想来写,但是,也要知道应该在哪里改最方便才行。
但是看zen framework这些东东的时候就觉得自己是菜鸟了。杯具!
用header("Location: manage/index.php");
MVC的发展是web开发一个重要理念和方法.如果你是小站,使用MVC是没有必要的,用了MVC反而到是"负担".而稍大一点的网站,不使mvc结构.你就会发现,有多晕了.
PHP 的 Redirect 更灵活.[code=PHP]
<?php
$url = "http://www.csdn.net";
header("Location: $url");
exit;
?>
这个垃圾代码也太多了,一共4行,有三行是没用的
<?php header('Location: http://www.csdn.net');?>
咳咳,那个exit是需要滴
就是没带ORM,我觉得php实现orm性能实在是不咋地,特别是active record那样的orm
我一般抽象出一个DAL供model调用,然后自己写sql语句实现
弱类型和OO没有必然关系。
只不过弱类型可以依靠duck typing,用不到abstract class和interface,这两个东西出现在php里有点多余,最多起到一点规范代码的作用
对于框架仍旧是十分陌生和反感的。现在我有自己的常用代码,比较熟练这些代码,基本上不会遇到解决不了的处理,代码是来自discuz的,然后自己另外加了一些,也修改了下。算是用的discuz的“框架”吧,但不是标准的。
试下thinkphp是中文的框架
适合看别人写的代码,discuz、WordPress、phpwind
有的时候感觉自己还挺牛的,但是有的时候却感觉到什么都不是,菜得要命。。
http://topic.csdn.net/u/20101008/16/c5e0c6be-d3f3-48b9-8934-391e5c9a355e.html
discuz和uchome的代码 让我感觉到自卑
我觉得中国人(其实是亚洲人)和欧美人的思维确实很不相同。
看WordPress,里面无需的代码一大堆,代码一行行的却是看上去技术很牛逼一样。
我还是比较喜欢国人自己开发的PHP程序。我觉得discuz 和 uchome 写的很不错,比WordPress强多了。
另外,国外一个火爆的BBS程序phpBB什么的,相当于国内的discuz程序。
晕菜,你装他还得特殊的环境配置,动不动就执行超时,而discuz是从来就没有的。
感觉很多人,尤其是菜鸟和初级的,看到什么都觉得国外的好。
是的,可是HR一听说连邮箱正则就不会写就PASS了。可惜我一直用isEmail
include_once("incClass.php");class DAO{
protected $conn;
protected $table;
public $errors;
function __construct($table){
$this->conn = getConn();
$this->table = $table;
$this->errors = false;
}
function get($Id){
$rs = $this->conn->execute("select * from $this->table where id =$Id");
return $rs->GetArray();
}
function save($fields,$Id = false){
if (!$Id) {
//insert
$rs=$this->conn->Execute("SELECT * FROM $this->table");
$insertSQL = $this->conn->GetInsertSQL($rs, $fields);
$this->conn->Execute($insertSQL);
if ($this->conn->ErrorMsg()) $this->errors = $this->conn->ErrorMsg();
}
else {
//update
$rs=$this->conn->Execute("SELECT * FROM $this->table where id=$Id");
$updateSQL = $this->conn->GetUpdateSQL($rs, $fields);
$this->conn->Execute($updateSQL);
//if ($this->conn->ErrorMsg()) $this->errors= $this->conn->ErrorMsg();
}
}
function delete($Id){
$sql="delete from $this->table where id=$Id";
$this->conn->execute($sql);
if ($this->conn->ErrorMsg()) $this->errors= $this->conn->ErrorMsg();
}
}class IdbDao extends DAO {
function __construct(){
parent::__construct("idb");
}
function delete($id){
$sql = "delete from amendsdetaildb where Iid=$id";
$this->conn->execute($sql);
parent::delete($id);
}
}class AdmendsDetailDbDAO extends DAO {
function __construct(){
parent::__construct("amendsdetaildb");
}
function getDetail($Iid){
$rs = $this->conn->execute("select * from $this->table where Iid =$Iid");
return $rs->GetArray();
}
}class ObjectDbDAO extends DAO {
function __construct(){
parent::__construct("objectdb");
}
}?><?php
include_once("incClass.php");
include_once("DAO.php");
include_once("num2rmb2.php");
class LawCase{
private $caseNum;
private $registerTime;
private $endTime;
private $investigation;
function __construct($caseNum,$registerTime,$endTime,$investigation){
$this->caseNum = $caseNum;
$this->endTime = $endTime;
$this->investigation = $investigation;
$this->registerTime = $registerTime;
}
function putOnRecord(){
}
function endACase(){
} function makeFiles(){
}
}class Investigation{
protected $investInfo;
protected $idbDao;
protected $Iid;
function __construct(){
$this->idbDao = new IdbDao();
}
function getInvestInfo(){
return $this->investInfo;
}
function setInvestInfo($arr){
$this->investInfo = $arr;
}}class EInvestigation extends Investigation {
private $highwayEstates;
private $admends;
private $detail;
private $admendsDetail;
private $EIinfo;
private $EIid;
private $ADDAO;
private $IDAO; function __construct($id = false){
parent::__construct();
$this->highwayEstates = false;
$this->EIinfo = false;
$this->EIid = $id;
$this->ADDAO = new AdmendsDetailDbDAO();
$this->IDAO = new IdbDao();
$this->admends = false;
$this->detail = false;
$this->admendsDetail = false;
}
function calculateAdmends(){
$this->initHighwayEstates();
$total = 0;
$detail = "";
$admendsDetail = "";
$sum = 1;
foreach ($this->highwayEstates as $key => $value) { $perTotal = $value["admendsPrice"]*$value["num"];
$detail .= $value["admendsName"].Transform($value["num"]).$value["admendsUnit"].";";
$admendsDetail .= $sum."、".$value["admendsName"].vsprintf("%1\$.0f",$value["num"]).$value["admendsUnit"]."×".
$value["admendsPrice"]."元/".$value["admendsUnit"]."=".$perTotal."元;";
$sum++;
$total = $total+$perTotal;
}
//$this->admends = vsprintf($total,"%1\$.2f");
$this->admends = $total;
$this->detail = $detail;
$this->admendsDetail = $admendsDetail;
}
function getAdmends(){
if (!$this->admends) {
$this->calculateAdmends();
$t = $this->admends;
}
else $t = $this->admends;
$t = vsprintf("%1\$.2f",$t);
return $t ;
}
function getDetail(){
if (!$this->detail) {
$this->calculateAdmends();
return $this->detail;
}
else return $this->detail;
}
function getAdmendsDetail(){
if (!$this->admendsDetail) {
$this->calculateAdmends();
return $this->admendsDetail;
}
else return $this->admendsDetail;
}
function initHighwayEstates(){
if (!$this->highwayEstates) {
if (!$this->EIid) {
return "没有赔偿明细。";
}else {
$this->highwayEstates = $this->ADDAO->getDetail($this->EIid);
}
}
}
function initEInfo(){
if (!$this->EIinfo) {
if (!$this->EIid) {
return "没有赔偿明细。";
}else {
$this->EIinfo = $this->IDAO->get($this->EIid);
}
}
}
function getReportInfo(){
$this->initEInfo();
$this->initHighwayEstates();
$fields = array();
$fields = $this->EIinfo[0];
$fields["admends"] = $this->getAdmends();
$fields["detail"] = $this->getDetail();
$fields["admendsDetail"] = $this->getAdmendsDetail();
//date("Y-m-d,H:i",strtotime($rs->fields["endTime"]));
$fields["investDate"] = date("m月d日",strtotime($fields["investTime"]));
$fields["investTime"] = date("H时i分",strtotime($fields["investTime"]));
$fields["breakDate"] = date("m月d日",strtotime($fields["breakTime"]));
$fields["breakTime"] = date("H时i分",strtotime($fields["breakTime"]));
$fields["endDate"] = date("m月d日",strtotime($fields["endTime"]));
$fields["endTime"] = date("H时i分",strtotime($fields["endTime"]));
return $fields;
}
function getRepairInfo(){}
}function encoderTransferForArray($s,$f,$a){
//$value = iconv($s,$f,$value);
foreach ($a as $key => $value) {
$a[$key] = iconv($s,$f,$value);
}
return $a;
}
?>
同意,能够运行就行。看到那些喜欢用OOP(滥用OOP) 和MVC的人写的那么一大堆臃肿的代码就觉得烦。