Reputation: 573
I have a class Club{} that has several public functions and private functions. The three private functions I have are used to get data from my database. Here is the following code:
private function Get_Members_From_DB()
{
$sql = "SELECT
Email, FirstName, LastName, Gender
FROM
member";
$result = mysqli_query($this->Con, $sql);
$arrayResult = array();
while($row = mysqli_fetch_array($result))
{
$arrayResult[] = $row;
}
return ($arrayResult);
}
private function Get_Members_Interests_From_DB($MemberEmail)
{
$sql = "SELECT
interest_type.InterestDescription
FROM
member, member_interests, interest_type
WHERE
member.Email = '$MemberEmail'
AND
member.Email = member_interests.Email
AND
member_interests.InterestID = interest_type.InterestID";
$result = mysqli_query($this->Con, $sql);
while($row = mysqli_fetch_array($result))
{
$arrayResult[] = $row;
}
return ($arrayResult);
}
private function Get_Interests_Types_From_DB()
{
$sql = "SELECT
InterestID,
InterestDescription
FROM
interest_type";
$result = mysqli_query($this->Con, $sql);
while($row = mysqli_fetch_array($result))
{
$arrayResult[] = $row;
}
return ($arrayResult);
}
When I call these, I get Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
on line 406, which is while($row = mysqli_fetch_array($result))
. I also get Fatal error: Cannot use object of type Club as array in
on line 48 which is echo("<tr><td>" . $row1['FirstName'] . " " . $row1['LastName'] . "</td><td>" . $row1['Email'] .
I have tried several versions of this, and I cannot seem to get the data to work into the following function:
public function DisplayMembers()
{
//$members[] = array();
$interests[] = array();
$members = new Club();
$members->Get_Members_From_DB();
echo ("<table id='membertable'><tr><td colspan='4'>Informatics Club Members</td></tr>
<tr><td width='130px'>Name</td><td width='170px'>Email</td><td width='60'>Gender</td>
<td width='280px'>Interests</td></tr>");
while($row1 = $members)
{
echo("<tr><td>" . $row1['FirstName'] . " " . $row1['LastName'] . "</td><td>" . $row1['Email'] .
"</td><td>" . $row1['Gender'] . "</td><td><ul>;");
$results2 = new Club;
$results2->Get_Members_Interests_From_DB($row1['Email']);
while($row2 = mysqli_fetch_array($results2))
{
$interests[] = $row2;
echo("<li>" . $row2['InterestDescription'] . "</li>");
}
echo("</ul></td></tr>");
};
echo "</table><br>";
}
I had changed private function Get_Members_From_DB()
to:
$sql = "SELECT
Email, FirstName, LastName, Gender
FROM
member";
$result = mysqli_query($this->Con, $sql); return ($result);
And it still does not work in the DisplayMembers()
function with out getting the first error I posted above, except it would be on line 46, which would be changed to: while($row1 = mysqli_fetch_array($members))
If I take the above code and paste it directly into DisplayMembers()
I can get the function to display correctly as an HTML table. I do not understand why I am getting errors when I separate the code into the private function. Any ideas?
EDIT: I am calling these private functions from a public function inside the SAME class.
Upvotes: 2
Views: 27451
Reputation: 41320
In php7 the anonymous classes were introduced. All you need is to create a class extending the class with private function. Then you can add any functions to it and event create an accessor for the private functions. You would also need to create a converter to convert your object of the old class into an object of the new class and that is it.
That is all you need.
You could do it without php7, but with php7 you can avoid creating class definition somewhere else. With php7 you can do all conversion in one function or in one class method. Isn't it cool?
Upvotes: 2
Reputation: 20889
I will help you to analyse your code, section by section. The first part looks like this...
$members = new Club();
$members->Get_Members_From_DB();
Get_Members_From_DB()
returns a value, but you do not store this value anywhere. Moreover, you're already within the Club class so we don't need a new Club. Instead try this...
$members = $this->Get_Members_From_DB();
Next up, you have...
while($row1 = $members)
But you're just assigning (copying) $members
to $row1
here. And as long as $members
is truthy this will loop forever. Instead, consider this...
foreach ($members as $member)
{
// Do something with $member
}
And finally, you have a similar problem getting your interest results. Your code is...
$results2 = new Club;
$results2->Get_Members_Interests_From_DB($row1['Email']);
while($row2 = mysqli_fetch_array($results2))
{
$interests[] = $row2;
echo("<li>" . $row2['InterestDescription'] . "</li>");
}
Again, Get_Members_Interests_From_DB()
is returning something, but you're not storing the value. How about this instead...
$interests = $this->Get_Members_Interests_From_DB($member['Email']);
foreach ($interests as $interest)
{
echo '<li>', $interest['InterestDescription'], '</li>';
}
Upvotes: 6
Reputation: 20889
You cannot access 'private' functions outside of the class they're defined in. As Simon has commented, you need to make these public.
You should read about visibility in the PHP Manual, it will teach you about the differences between public, protected and private functions. Here's a short example:
class Example {
public function aPublicMethod()
{
return 'public';
}
private function aPrivateMethod()
{
return 'private';
}
}
$example = new Example;
// $aPublicString will become 'public'
$aPublicString = $example->aPublicMethod();
// error, you cannot see private functions outside of a class
$aPrivateString = $example->aPrivateMethod();
Upvotes: 1