通过PHP打印长HTML是否正确?
我想知道使用echo通过php类打印长html是否正确,而不是仅以普通方式打印,例如,执行以下操作: 谢谢,我有一个用户类通过PHP打印长HTML是否正确?,php,html,oop,Php,Html,Oop,我想知道使用echo通过php类打印长html是否正确,而不是仅以普通方式打印,例如,执行以下操作: 谢谢,我有一个用户类 User.php class User { private $name, $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function printPro
User.php
class User {
private $name, $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function printProfile($name, $age) {
echo '<div class="panel panel-profile">
Name: '.$this->name.'<br />
Age: '.$this->age.'<br />
</div>';
}
}
User.php
类用户{
私人$name,$age;
公共函数构造($name,$age){
$this->name=$name;
$this->age=$age;
}
公共函数printProfile($name,$age){
回声'
名称:'.$this->Name.'
年龄:'.$this->Age.
';
}
}
我的profile.php看起来像
profile.php
<html lang="en">
<head>
<?php
require_once __DIR__ . "/Head.php
$head = new Head();
$head->printPanel();
?>
</head>
<body>
<?php
require_once __DIR__ . "/Header.php
$header = new Header();
$header->printPanel();
?>
<div class="content">
<?php
require_once __DIR__ . "/User.php
$user = new User();
$user->printProfile();
?>
</div>
<?php
require_once __DIR__ . "/Footer.php
$footer = new Footer();
$footer->printPanel();
?>
</body>
</html>
<?php require_once __DIR__ . "/header.php ?>
profile.php
所以。。我知道这是可能的,但我不知道这样做是否正确,我是否正确使用了代码?会影响处理速度吗?
我喜欢这种方式,因为我可以减少所有页面中的代码,只需更改类,它就可以修改所有代码。我也知道我可以做一些像
profile.php
<html lang="en">
<head>
<?php
require_once __DIR__ . "/Head.php
$head = new Head();
$head->printPanel();
?>
</head>
<body>
<?php
require_once __DIR__ . "/Header.php
$header = new Header();
$header->printPanel();
?>
<div class="content">
<?php
require_once __DIR__ . "/User.php
$user = new User();
$user->printProfile();
?>
</div>
<?php
require_once __DIR__ . "/Footer.php
$footer = new Footer();
$footer->printPanel();
?>
</body>
</html>
<?php require_once __DIR__ . "/header.php ?>
使用echo将html输出到屏幕上现在更为典型
PHP echo和print语句echo和print或多或少是相同的
一样。它们都用于向屏幕输出数据
差别很小:echo没有返回值,而print有
返回值1,以便在表达式中使用。echo可以
打印时可以使用多个参数(尽管这种用法很少)
一个论点。回音比打印速度稍快一些
你可以阅读更多关于这方面的内容
您应该将require导入添加到页面顶部。大多数人都是这样做的 事实证明,面向对象并不是一切。如果你能在程序上做同样多的事情,那就去做吧。在您的情况下,对于一个简单的网站,您可以将所有块存储在一个单独的文件中,例如,chunks.php
。对要添加的每个部分使用一个函数。页面上的每个元素不需要20个不同的类和构造函数。只需使用静态方法
至于代码样式,请将所有要求放在顶部,这样页面不会在渲染中途崩溃:
<?php require_once __DIR__ . "/Chunks.php ?>
<html lang="en">
<head>
<?php Chunks::printHead(); ?>
</head>
<body>
<?php Chunks::printHeader(); ?>
<div class="content">
<?php Chunks::printUserProfile(); ?>
</div>
<?php Chunks::printFooter(); ?>
</body>
</html>
谢谢!您的答案非常有用,我将开始使用更多的静力学方法。
我一直在做一些简单的网站,使用javascript、jquery、html和php,但我即将开始一个更大的项目,我想澄清一些东西。
你说在你的情况下,对于一个简单的网站,但是对于一个更大的网站呢?这同样适用?这只是编码风格。你把东西放的顺序对性能没有任何影响。我认为大多数程序员会认为最好把所有的<代码>都要求语句和变量赋值在开始,只需调用HTML块中的函数。在您的情况下,您只是从不同的文件加载片段,以保持视图干净。通常,您需要特定于对象的方法来证明面向对象方法的合理性。