PHP OOP vs Inline


PHP OOP vs Inline

我拼命地想转向OOP,但就是不知道什么时候使用它。我得到了机制,但何时使用它们只是不点击。我很好奇我目前的方案是否适合采用 OOP 方法。

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

我是新手,但我觉得骨子里有一种更好的方法来组织这段代码。我会研究一个框架,但我已经深入这个项目,所以正在寻找关于如何更好地分解它的 OOP 想法,或者验证我正在以尽可能简化的方式做到这一点。所有评论都有帮助!

详。.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;
?>

.加载说明。.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>

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

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

这使您可以更好地组织和重用功能,而无需代码复制或搜索代码库。例如,假设我想开始在所有页面上以特定方式打印每个笔记的插入日期。我该怎么做?我可能不得不更改网站上的每个页面。

如果我们有正确定义的二传手和获取者,这将成为一个非常简单的任务。我只需要在 1(非常明显(位置替换格式化的返回字符串:

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

诚然,这一切在小规模上似乎都非常过分和耗时。我记得在大学时为自己建立了一个个人网站,当时我有丰富的OO经验。当时我正在学习PHP,对于这个网站,我倾向于使用内联代码只是为了速度。它工作正常,非常快速和轻巧。我不理解Web框架的喧嚣,因为它们感觉过于"沉重"。

在维护过程中,事后出现问题。当你在 6 个月或几年后返回代码时,你试图弄清楚这个调用在哪里,或者为什么你必须在 8 个地方更改它来修复错误。这些是由不良耦合引起的感觉 - 代码库中的内聚。

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

我希望这有所帮助。如果我错过了什么,请告诉我,我会根据需要更新。