PHP 过程式到 OOP
我正在尝试将程序代码转换为 oop。
<?php
$dbc = get_dbc();
$info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
while ($info_row = mysqli_fetch_array($info))
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
?>
<div style="width: 100%;">
<div style="float: left;">
<?php echo $info_id; ?>
</div>
<div style="float: left;">
<?php echo $info_title; ?>
</div>
<div style="clear: both;"></div>
</div>
<?php } ?>
我对没有 HTML 样式的类/对象的不完整尝试:
<?php
class InfoTest {
private $info_id;
private $info_title;
public function __construct() {
$dbc = get_dbc();
$info = $dbc->query ("SELECT info_id, info_title FROM text");
if ($dbc->error) {
printf("Error: %s\n", $dbc->error);
}
while ($info_row = $info->fetch_array())
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
}
$info->free();
$this->info_id = $info_id;
$this->info_title = $info_title;
}
public function setInfoID() {
$this->info_id = $info_id;
}
public function getInfoID() {
return $this->info_id;
}
public function setInfoTitle() {
$this->info_title = $info_title;
}
public function getInfoTitle() {
return $this->info_title;
}
public function __destruct() {
}
}
?>
<?php
$display = new InfoTest();
echo $display->getInfoID();
echo $display->getInfoTitle();
?>
我的程序代码打印出:1 一 2 二。
我的 oop 代码打印出: 2 二
根据我的理解,oop 是这样打印的,因为 $info_id 和 $info_title 不是数组,并且只打印出最后存储的信息。
因此,如果我将: 更改
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
为:
$info_id[] = $info_row['info_id'];
$info_title[] = $info_row['info_title'];
并打印数组,它会显示我想要的所有信息,但如何以非数组形式显示它?
到目前为止我所做的事情是正确的还是我的做法是错误的?
I'm trying to convert my procedural code to oop.
<?php
$dbc = get_dbc();
$info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
while ($info_row = mysqli_fetch_array($info))
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
?>
<div style="width: 100%;">
<div style="float: left;">
<?php echo $info_id; ?>
</div>
<div style="float: left;">
<?php echo $info_title; ?>
</div>
<div style="clear: both;"></div>
</div>
<?php } ?>
My incomplete attempt at classes/objects without the HTML styling:
<?php
class InfoTest {
private $info_id;
private $info_title;
public function __construct() {
$dbc = get_dbc();
$info = $dbc->query ("SELECT info_id, info_title FROM text");
if ($dbc->error) {
printf("Error: %s\n", $dbc->error);
}
while ($info_row = $info->fetch_array())
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
}
$info->free();
$this->info_id = $info_id;
$this->info_title = $info_title;
}
public function setInfoID() {
$this->info_id = $info_id;
}
public function getInfoID() {
return $this->info_id;
}
public function setInfoTitle() {
$this->info_title = $info_title;
}
public function getInfoTitle() {
return $this->info_title;
}
public function __destruct() {
}
}
?>
<?php
$display = new InfoTest();
echo $display->getInfoID();
echo $display->getInfoTitle();
?>
My procedural code prints out: 1 One 2 Two.
My oop code prints out: 2 Two
From my understanding the oop prints out that way because $info_id and $info_title aren't arrays, and only print out the last stored information.
So, if I change:
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
To:
$info_id[] = $info_row['info_id'];
$info_title[] = $info_row['info_title'];
And print the arrays, it displays all the information I want, but how to display it in non-array form?
Is what I'm doing so far correct or am I approaching this wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你做错了。在您的程序示例中,您一次迭代一行数据;在面向对象的示例中,如果将它们视为数组然后打印它们,那么您将一次浏览一列数据。我不会将数据分成单独的 id 和标题,而是将它们视为一个捆绑包(即类似于您在程序版本中的做法)- id 与标题一起使用,而不是其他 id,对吗?
因此,例如,您可能有一个成员变量
,然后在构造函数中执行以下操作:
然后提供一个方法来获取此数组数组:
最后,您可以对其进行迭代,与过程示例中的操作非常相似:
退后一步——你可能会问这一切是否真的有必要。过程式 PHP 本质上并没有什么问题——如果它做了您需要它做的事情并且做得很清楚,那么在这里您可能会更好地选择简单而不是复杂。
You're doing it wrong. In your procedural example you're iterating over the data a row at a time; in your OO example, if you treat them as arrays and then print them, you're going through the data a column at a time instead. Rather than separating the data into separate ids and titles, I would treat them as a bundle (i.e. similar to how you did it in the procedural version) - an id goes with a title, not other ids, right?
So, for example, you might have a member variable
and then in your constructor, do:
and then provide a method to get at this array of arrays:
Finally, you could iterate over it very similarly to how you did in the procedural example:
Stepping back - you could ask if all this is really necessary. There's nothing inherently wrong with procedural PHP - if it does what you need it to do and does it clearly, you might be better off favoring simple over complex here.
因为 info id 是对象中的一个数组,所以获取该值的相应函数应该采用偏移量。或者更好的是,您应该查看实现迭代器的类,以便您可以对对象执行 foreach
Because info id is an array in your object the corresponding function to get the value should take an offset. Or even better you should look at your class implementing iterator so that you can just do foreach over your object
在切换到 OOP 之前,我首先将代码模块化并开始分割成逻辑彼此分离的功能部分,例如数据库访问和模板化:
然后将与数据库相关的功能移动到它自己的文件中,以将其与“模板”。完成后,您可以考虑进一步的重构步骤。我建议阅读以下内容(仅独立于命名框架): 当扁平 PHP 遇到 symfony 。
Before switching to OOP I would first of all modularize the code and start to segment into functional parts that have separated logic from each other, e.g. database access and templating:
Then move the database related functions into a file of it's own to decouple it from the "templates". After that's done you can think about further steps to refactor. I suggest the following read (which is merely independent to the named framework): When flat PHP meets symfony.