PHP OOP Class Help
I'm am unsure on how to move part of my code into a class.
<?php
class InfoTest {
private $info_results;
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_results[]= $info_row;
}
$info->free();
$this->info_results = $info_results;
}
public function setInfo() {
$this->info_results = $info_results;
}
public function getInfo() {
return $this->info_results;
}
public function __destruct() {
}
}
?>
<?php
$display = new InfoTest();
foreach ($display->getInfo() as $info_row) {
?>
<!-- html -->
<?php echo $info_row['info_title']."</a><br />"; ?>
<!-- html -->
Sub-Info:
<?php
$dbc = get_dbc();
$si_title = $dbc->query ("SELECT info_title FROM text WHERE info_id = ".$info_row['info_id']."");
if ($dbc->error) {
printf("Error: %s\n", $dbc->error);
}
$num =$si_title->num_rows;
$count = 0;
while ($sub_info = $si_title->fetch_array())
{
$sub_info_title = $sub_info['info_title'];
if ($count!=$num-1)
{
echo $sub_info_title." , ";
$count++;
}
else echo $sub_info_title;
}
?>
<!-- html -->
<?php } ?>
I'm unsure how to move the Sub-Info(code after Sub-Info:) into a class. Does it go in the same class as InfoTest, a class of its own, or doesn't go into a class at all?
Sub-Info Code:
<?php
$dbc = get_dbc();
$si_title = $dbc->query ("SELECT info_title FROM text WHERE info_id = ".$info_row['info_id']."");
if ($dbc->error) {
printf("Error: %s\n", $dbc->error);
}
$num =$si_title->num_rows;
$count = 0;
while ($sub_info = $si_title->fetch_array())
{
$sub_info_title = $sub_info['info_title'];
if ($count!=$num-1)
{
echo $sub_info_title." , ";
$count++;
}
else echo $sub_info_title; 开发者_Go百科
}
?>
In your class you have already all information. So an alternative to a sql-query could be an additional method, which searches all titles with a special id in the private field info_results
. E.g.:
public function getInfoTitles($info_id) {
$titles = array();
foreach ($this->info_results as $info_row) {
if ($info_row['info_id'] == $info_id)
$titles[] = $info_row['info_title'];
}
}
return $titles;
}
Your Sub-Info Code is then:
echo implode(', ', $display->getInfoTitles($info_row['info_id']));
The general idea of OOP is to couple data with methods that process that data. So, if you feel that some piece of your data are processed in the same way multiple times, it's a good idea to introduce a class that will incapsulate that data and logic.
Of course it emerges a lot of other questions: how many classes should one have, how should they interact with each other, which part of business logic should go in which class etc. There is no universal, always-true answer to that questions, but some general approaches to address that questions were developed: the design patterns. There are some books on the topic, one of the most known is Gang-of-Four (GoF) Design Patterns.
That's general thoughts on the topic. In your particular case, I would suggest you creating new class ItemInfo
, so InfoTest
class is responsible only for quering the DB and creating Instances of this new class.
class InfoTest {
private $items;
public function __construct() {
$this->items = new Array();
}
private function queryItems($itemId){
$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())
{
$item = new ItemInfo($info_row);
$this->items[] = $item;
}
$info->free();
}
public function getItems($itemId){
if (empty($this->items)){
$this->queryItems($itemId);
}
return $this->items;
}
/* Other functions. */
public function __destruct() {
}
}
Class ItemInfo{
private $id, $title;
function __construct(Array $params){
$this->id = $params['item_id'];
$this->title = $params['item_title'];
}
function getTitle(){
return $this->title;
}
function toString(){
retirn "I'm item {$this->id}, my title is {$this->title}";
}
}
And your code will be as simple as
$item_test = new ItemTest();
$items = $item_test->getItems($item_id);
$titles = array();
foreach ($items as $item){
//you may process your items in any way you need
$titles[] = $item->getTitle();
}
echo implode(',', $titles);
精彩评论