2012-05-14 65 views
0

我正拼命地朝着OOP方向发展,但我无法将自己的头包裹在何时使用它。我得到的机制,但何时使用它们只是不点击。我很好奇,如果我目前的方案已经成熟的OOP方法。PHP OOP vs Inline

我有3页。 Details.php显示了两个并排的div。用户可以在其中添加一个笔记,另一个用户可以在其中查看存储在MySQL中的以前的笔记。他们可以通过Details.php中的AJAX函数添加笔记并提取笔记。 javascript函数调用add_notes.php来向数据库添加注释,并调用load_notes.php通过Jquery .load()加载页面上的注释,以及当提交新注释刷新div时。

我是一个新手,但我觉得在我的骨头里有一个更好的方法来组织这段代码。我会研究一个框架,但是我对这个项目深感不安,所以寻找OOP的想法来更好地解决这个问题,或者验证我正在以尽可能精简的方式进行操作。所有评论都很有帮助!

DETAILS.PHP

<script type="text/javascript"> 
$(document).ready(function(){ 
//When loading page load notes/messages tables and then reload when ajax is done   
$('#note_holder').load('load_notes.php?subcat=<? echo $subcat;?>'); 
    //onclick handler send message btn 
    $("#notes_submit").click(function(){ 
     $(this).closest('form').submit(function(){ 
      return false; 
     }); 
     var frm = $(this).closest('form');    
     var data = $(frm).serialize(); 
      if($(frm).valid()){         
        $.post( 
          "../php/add_notes_ajax.php", 
          data, 
          function(data){        
           $('#note_holder').load('load_notes.php?subcat=<? echo $subcat;?>'); 
          } 
        ); 
      } 
    });   
}); 
</script> 

<div style="float:left; margin-left:15px;"> 
    <form name="messages1" class="form" id="myforma" method="post" action="#" enctype="multipart/form-data"> 
     <fieldset style="width:500px; height:400px; overflow:auto; font-size:11px;"> 
      <legend>Click to View Previous Notes/Messages</legend>    
      <div style="height:350px; overflow:auto;" class="note_holder" id="note_holder"> 
      <!--This div is being called from the ajax script to load add_notes_ajax.php-->    
      </div>   
     </fieldset> 
     <div style="margin-top:20px;"></div> 
    </form>  
</div> 

<div style=" float:right;"> 
    <form name="notes" class="notes" id="notes" method="post" action="#" enctype="multipart/form-data"> 
    <fieldset style="width:300px; height:400px;"> 
     <legend>Enter a Note</legend> 
     <div style="margin-top:00px;"></div> 
     <div> 
    <textarea rows="20" cols="20" style="height:300px; width:290px;" name="notes"></textarea> 
    <input type="submit" name="notes_submit" id="notes_submit" value="Submit Note" class="button" /> 
    <input type="hidden" name="subcat" value= "<?php echo $subcat; ?>" /> 
     </div> 
    </fieldset> 
    <div style="margin-top:20px;"></div> 
    </form> 
</div> 

笔录AJAX.PHP

<?php 
include_once('../bootstrap.php'); 
include_once('../site_globals/common_functions.php'); 
include_once('../site_globals/common_queries.php'); 
include_once('../php/gump.class.php'); 
page_protect(); 
error_reporting(0); 

$firstname = filter($_SESSION['user_name']); 
$myid  = filter($_SESSION['user_id']); 

// All the variables from the submission form 
$notes  = filter($_POST['notes']); 
$subcat = filter($_POST['subcat']); 

//Insert Notes into the database 

    $stmt = $dbh->prepare(' 
     INSERT INTO `notes` 
      (date , sub_cat_id , notes) 
     VALUES 
      (:date , :subcat , :notes) 
      '); 
    $stmt->bindValue('subcat', $subcat); 
    $stmt->bindValue('date', date('Y-m-d H:i:s')); 
    $stmt->bindValue('notes', $notes); 
    $stmt->execute();  

echo "This note was added successfully"; 
exit; 

?> 

。 LOAD NOTES.PHP

<table width="100%"> 
    <thead style="text-align:left; "> 
    <tr style="font-size:14px; font-weight:bold;"> 
     <!-- <th><input class="check-all" type="checkbox" /></th>--> 
     <th>Date</th> 
     <th >Contents</th> 
     <th>Preview/Print</th> 
    </tr> 
    </thead> 
    <?php while ($messages_row = mysql_fetch_object($messages_res)):?> 
    <tr> 
    <td><a target="_blank" href="../site_hospital_files/thread.php?question_id=<?php echo $messages_row->question_id;?>"><?php echo substr($messages_row->reply, 0, 20) . '...';?></a></td> 
    <td><?php echo date('Y-m-d', strtotime($messages_row->date_added));?></td> 
    <td><a href="../site_hospital_files/pdf_messages_notes.php?msg_id=<?php echo $messages_row->question_id;?>&amp;var1=<?php echo $subcat;?>">Create PDF</a></td> 
    </tr> 
    <?php endwhile;?> 
    <?php while($notes_row = $notes_res->fetch(PDO::FETCH_ASSOC)):?> 
    <tr> 
    <td><?php echo $notes_row[date]; ?></td> 
    <td><?php echo substr($notes_row[notes], 0, 50).'...';?></td> 
    <td><a href="pdf_messages_notes.php?note_id=<?php echo $notes_row->sub_cat_id; ?>&var1=<?php echo $subcat;?>">View</a></td> 
    </tr> 
    <?php endwhile;?> 
</table> 
+0

在这一点上我不会太在意。总是有改进的方法,但你所做的并不差。很高兴看到您使用PDO! –

+2

...但为什么随机'mysql_fetch_object'? – deceze

+1

这个问题更适合于http://codereview.stackexchange.com,而不是在这里。 – deceze

回答

3

这绝对是。鉴于MySQL和其他关系数据库的关系性质,定义您的PHP对象和mysql表的代码表示非常容易。考虑这种过于简单的类:

<?php 
    class Note { 
     private $id 
     private $text; 
     private $insert_dt; 
     private $update_dt; 
    } 
?> 

这是什么让你做的是更好地组织和无需在你的代码库中的代码复制或狩猎重用功能。例如,假设我想要在所有页面上以特定方式开始输出每个笔记的插入日期。我将如何做到这一点?我可能必须更改网站上的每个页面。

如果我们有正确定义的setter和getter,这成为一项非常简单的任务。我应该只需要更换格式返回字符串在1(非常明显)位置:

<?php 
    class Note { 
     // ... 
     public function getFormattedInsertDate() { 
      return date("M, Y", $this->insert_dt); 
     } 
    } 
?> 

诚然,这一切似乎很过分的和费时的小规模。我记得当我有丰富的面向对象方面的经验时,在大学里为自己建立一个私人网站。我当时在学习PHP,为了速度,我倾向于使用内联代码。它运行良好,非常快速和轻量级。我不明白网页框架的喧嚣,因为他们感觉过度“沉重”。

问题出现在维护期间之后。当您在6个月或数年后返回代码时,您可以尝试找出此通话的位置,或者为什么必须在8个位置更改以修复错误。这些是由于糟糕的耦合 - 你的代码库中的内聚引起的感觉。

幸运的是,多年来出现了许多框架,不仅支持,而且鼓励和强化这种行为。如果你没有,我会强烈建议寻找CakePHP或Code Igniter。它们都非常易于精简框架,这些框架确实很好地指出了这些概念,并提供了出色的入门教程来引导您创建博客网站。

我希望这会有所帮助。如果我错过了任何内容,请告知我,我会根据需要更新。

+0

感谢您的帮助Ben!那么在我的例子中,你会把所有东西都移动到一个类中?你会把javascript和所有东西都放到一个类中,然后调用echo $ newnote-> show_notes();在你的主页上? Geez,我觉得我已经接近了解何时使用类,但由于某种原因不能完全达到目的。 –

+1

真正的目标是让自己的方式来模型视图控制器,但再次,为了简化,我会说如果你从数据库拉,该表应该有一个类。将您的客户语言(html,css,js)保存在自己的文件中。当你需要数据时,如你所建议的,只需引用该类。 –

+0

好的,这有助于很多。谢谢!我希望我会从一个框架开始,但我并没有预料到我的第一个项目会如此之大。现在我的膝盖很深,我需要找到向正确方向前进的方法。建议有帮助。 –