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>";
?>