duansengcha9114 2015-11-01 12:04
浏览 51

在Object Operator( - >)之后使用Scope Resolution Operator(::)或再次实例化同一个类是一个坏习惯吗?

I couldn't find a similar question, so I apologize if it's already been answered. And sorry for the possibility of wrong formatting, I still don't get how it works on SO :/

Before I'm asking the question, I'm trying to scenario I've been in yesterday.

First off we have a class called "User" with 2 functions "Fu" and "Bar in a document called Funtions.php:

Class User
{
 Public Function Fu($Variable)
 {
  //Do something
 }
 Public Function Bar($Variable)
 {
  //Do something else with the same variable
 }
}

What those functions do is not relevant to the question (so far I know).

In another document that calls to the class twice I tried this:

$UserAction = new User();
$Variable = "Please don't crash";

$Action1 = $UserAction->Fu($Variable);
//This line ^ works, works as intended

$Action2 = $UserAction->Bar($Variable);
//This line ^ results an fatal error: Fatal error: Call to a member function Bar() on a non-object

I found 2 working solutions for this error.
Solution A:
Changing $Action2 = $UserAction->Bar($Variable); to $Action2 = User::Bar($Variable);

Solution B is instantiate the same class twice, like this:

$UserAction = new User();
$Variable = "Please don't crash";

$Action1 = $UserAction->Fu($Variable);

$UserAction = new User();
$Action2 = $UserAction->Bar($Variable);

And now finally the question I'm trying to ask: While both solutions work, I was wondering is there a better way to solve this problem? Are solutions A and B bad practices?

EDIT:

The real code. Keep in mind the variable names are different then the one in the scenario above. Also its in Dutch, I don't know if its better to translate it to English or not.

Functies.php (Functions.php):

<?php
 include_once 'Config.php';
 class Gebruiker
 {
 public function __construct() 
 {
  $db = new DB_Class();
 }
 public function get_antwoord($vraagje)
 {
  $query = mysql_query("SELECT * from antwoorden WHERE vraagID = '$vraagje'");

  $_SESSION['GetAntwoord'] = true;
  return $query;
 }
 public function get_voledigevraag($vraagje)
 {
  $query = mysql_query("SELECT * from vragen WHERE vraagid = '$vraagje'");
  $rijen = mysql_num_rows($query);

  if (!$rijen == 0) 
  {
   return $query;
  }
 }
 public function get_vraag()
 {
  $query = mysql_query("SELECT * from vragen");

  $_SESSION['GetVraag'] = true;
  return $query;
 }
}
?>

The other document that's trying to get the functions from Functions.php (Vraag.php):

<?php
 session_start();
 include('Functies.php');
 include('nav.php');
 include('bootstrap.php');
 $gebruiker = new Gebruiker();

 $vraagje = $_POST['verstopt'];
 $resultaat = $gebruiker->get_voledigevraag($vraagje);

 while(list($vraagID, $vraagtitel, $vraag, $datum, $gebruiker) = mysql_fetch_array($resultaat))
 {
  echo "<tr>";
  if(!empty($_SESSION['adminlogin'])){echo "<td><form action='bewerkvragen.php' method='post'><input type='hidden' name='verstopt' value=$vraagID><input type='submit' name='wijzig' value='wijzig'></form></td>";}
  if(!empty($_SESSION['adminlogin'])){echo "<td><form action='vragenverwijderen.php' method='post'><input type='hidden' name='verstopt' value=$vraagID><input type='submit' name='wijzig' value='verwijder'></form></td>";}
  echo "<td>$vraagtitel</td>";
  echo "<td>$vraag</td>";
  echo "<td>$datum</td>";
  echo "<td>$gebruiker</td></tr>";
}

 $antwoorden = Gebruiker::get_antwoord($vraagje);

 while(list($antwoordID, $antwoord, $datumantwoord, $gebruikerantwoord) = mysql_fetch_array($antwoorden))
{
  echo "<tr>";
  // if(!empty($_SESSION['adminlogin'])){echo "<td><form action='bewerkvragen.php' method='post'><input type='hidden' name='verstopt' value=$antwoordID><input type='submit' name='wijzig' value='wijzig'></form></td>";}
  // if(!empty($_SESSION['adminlogin'])){echo "<td><form action='vragenverwijderen.php' method='post'><input type='hidden' name='verstopt' value=$antwoordID><input type='submit' name='wijzig' value='verwijder'></form></td>";}
  echo "<td>$antwoord</td>";
  echo "<td>$datumantwoord</td>";
  echo "<td>$gebruikerantwoord</td></tr>";
}
?>

Where $_Post['Verstopt'] comes from:

<?php
 $resultaat = Gebruiker::get_vraag();

 while(list($vraagID, $vraagtitel, $vraag, $datum, $gebruiker) = mysql_fetch_array($resultaat))
 {
  echo "<tr><td><form action='vraag.php' method='post'><input type='hidden' name='verstopt' value=$vraagID><input type='submit' id='button' value='$vraagtitel'></form></td>";
  echo "<td>$datum</td>";
  echo "<td>$gebruiker</td>";

  if(!empty($_SESSION['adminlogin'])){echo "<td><form action='bewerkvragen.php' method='post'><input type='hidden' name='verstopt' value=$vraagID><input type='submit' name='wijzig' value='wijzig'></form></td>";}
  if(!empty($_SESSION['adminlogin'])){echo "<td><form action='vragenverwijderen.php' method='post'><input type='hidden' name='verstopt' value=$vraagID><input type='submit' name='wijzig' value='verwijder'></form></td>";}
  echo "</tr>";
 }
  echo "<tr><td colspan=12>";
  echo "<form action='invoervragen.php'><input type='submit' name='invoer' value='invoer'></form>";
  echo "<form action='index.php' method='post'><input type='submit' name='invoer'   value='terug naar home'></form></td></tr>";
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入