Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在面向对象的PHP设计中,HTML应该呈现在哪里?_Php_Oop - Fatal编程技术网

在面向对象的PHP设计中,HTML应该呈现在哪里?

在面向对象的PHP设计中,HTML应该呈现在哪里?,php,oop,Php,Oop,使用面向对象的PHP,HTML应该呈现在哪里 业务流程包括几个维护客户记录的操作。 每个业务流程的呈现是否应该获得一个单独的PHP文件?例如:viewCustomerTransactions.php 这样的代码应该驻留在哪里 $custTrans = Customer.getTransactions(); foreach ($custTrans as $ct){ $amount = $ct[0]; $date = $ct[1]; $product = $ct[2]

使用面向对象的PHP,HTML应该呈现在哪里

业务流程包括几个维护客户记录的操作。
每个业务流程的呈现是否应该获得一个单独的PHP文件?例如:viewCustomerTransactions.php

这样的代码应该驻留在哪里

$custTrans = Customer.getTransactions();

foreach ($custTrans as $ct){
     $amount = $ct[0];
     $date = $ct[1];
     $product = $ct[2];

     echo '<div class="custTrans">';
         echo '<span class="custTransAmount">'.$amount.'</span>';
         echo '<span class="custTransDate">'.$date.'</span>';
         echo '<span class="custTransproduct">'.$product.'</span>';
     echo '</div>';
}
$custrans=Customer.getTransactions();
外汇($custTrans as$ct){
$amount=$ct[0];
$date=$ct[1];
$product=$ct[2];
回声';
回显“.$amount.”;
回显“.$date.”;
回显“.$product.”;
回声';
}

也许像codeigniter这样的MVC框架会更好?

如果我是你,我会将html存储在一个变量中,而不是像这样呼出:

$custTrans = Customer.getTransactions();

$html = "";
foreach ($custTrans as $ct){
     $amount  = $ct[0];
     $date    = $ct[1];
     $product = $ct[2];

     $html .= "<div class="custTrans">";
         $html .= "<span class='custTransAmount'>".$amount."</span>";
         $html .= "<span class='custTransDate'>".$date."</span>";
         $html .= "<span class='custTransproduct'>".$product."</span>";
     $html .= "</div>";
}
这能解决你的问题吗,伙计


W.

我仍然在想什么是保持php和布局分离而不产生太多模糊的最佳方法。目前我非常喜欢include模板方法,因为它非常简单,没有任何限制

因此,对于您的示例,您将有一个php文件(example.php),该文件如下所示:

<?php
$custTrans = Customer.getTransactions();

$displ_transactions = array();

foreach ($custTrans as $ct){
     $transaction = array(
         'amount' => $ct[0],
         'date' => $ct[1];
         'product' => $ct[2];
     );
     $displ_transactions[] = $transaction; // this will push the transacion into the array
}
include 'example.tpl.php'
?>

然后需要第二个文件(example.tpl.php):


;
;
;
只需在浏览器中调用example.php,您将看到与以前相同的结果。 这对于小型网站都是好的,因为这种方法会导致一些开销。如果你是认真的模板,使用smarty。它很容易学习,并且有自动缓存,所以速度非常快

我只是意识到你也可以这样做:

example.php:

<?php
$custTrans = Customer.getTransactions();

foreach ($custTrans as $ct){
     $amount = $ct[0];
     $date = $ct[1];
     $product = $ct[2];
     include 'example.tpl.php';
}
?>

example.tpl.php:

<?php foreach ($displ_transactions as $transaction) { ?>
     <div class="custTrans">
         <span class='custTransAmount'><?php echo $transaction['amount'] ?></span>;
         <span class='custTransDate'><?php echo $transaction['date'] ?></span>;
         <span class='custTransproduct'><?php echo $transaction['product'] ?></span>;
     </div>
<?php } ?>
 <div class="custTrans">
     <span class='custTransAmount'><?php echo $amount ?></span>;
     <span class='custTransDate'><?php echo $date ?></span>;
     <span class='custTransproduct'><?php echo $product ?></span>;
 </div>

;
;
;

使用最适合您的工具:)

我必须确认include模板(Jules Colle提到)是答案之一,但是,当项目太大时,维护起来可能会很麻烦,所以请记住记录什么文件包含在什么文件中,因为(目前)没有(免费的)此类型链接的IDE解决方案。。。这是一个解决方案,它可以轻松地将您从一个文件带到另一个文件,或者简单地将所有内容放到类似过程的代码中

编辑: 不要忘记神奇的常数:

这是:
$\u SERVER['DOCUMENT\u ROOT']

可能重复:您的示例与OO PHP无关-您传递数组、遍历数组和回显结果,MVC提供了与渲染和数据的良好分离,我个人喜欢CI+1 for MVC。我也很喜欢CI。@Christian-是的,我想你是对的,我看的时候找不到,所以谢谢!礼仪:如果我问了一个重复的问题,我该怎么办?@jpwco:别担心重复的问题。如果有足够多的人同意这是一个骗局,他们会投票结束。这是个好主意,谢谢。然而,我问的更多的是从设计的角度来看,应该在什么地方进行回应,而不是如何将所有内容连接到一个$html中。干杯,我接受这个答案——也是对@Christian Joudrey的+1,感谢他给我的精彩答案中的敏锐提示
 <div class="custTrans">
     <span class='custTransAmount'><?php echo $amount ?></span>;
     <span class='custTransDate'><?php echo $date ?></span>;
     <span class='custTransproduct'><?php echo $product ?></span>;
 </div>