Fatal Error: Fatal error: Call to undefined function

Errors with my PHP code

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2877 Views - Last Post: 21 December 2010 - 01:28 PM Rate Topic: -----

#1 RUFerret  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 17-December 10

Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 09:12 AM

I'm creating a 'Potion System' and I've reached a bug.

Fatal error: Call to undefined function createPotion() in C:\wamp\www\iSystem.php on line 84

<?php
error_reporting (E_ALL ^ E_NOTICE);
class potion
{
	public $name;
	public $effect;
	public $HPSAP;
	public $MPSAP;
	public $poison;
	public $StatSAP;
	public function specs($tempname)
	{
		$this->name = $tempname;
		$this->HPSAP = false;
		$this->MPSAP = false;
		$this->poison = false;
		$this->StatSAP = false;
	}
	public function createPotion($potion)
	{
		if ($player->HPSAP == true)
		{
			echo "HP Sap added";
		}
		else if ($player->MPSAP == true)
		{
			echo "MP Sap added";
		}
		else if ($player->poison == true)
		{
			echo "poison added";
		}
		else if ($player->StatSAP == true)
		{
			echo "Stat Sap added";
		}
		echo $potion->name." has been created.";
	}
}
echo "Type Potion Name<br />";
?>
<form method ="post" action="iSystem.php" />
<input name="potion" />
<input type="submit" value="Create Potion" /><br /><br />
<?php
$potionname = $_POST['potion'];
$potion = new potion();
$potion->specs($potionname);
echo "Add what to ".$potion->name;
echo "<br /><br />";
?>
<form method ="post" action="iSystem.php" />
<input type="checkbox" name="effect" value="poison" />Poison<br >
<input type="checkbox" name="effect" value="HPSAP" />HP Sap<br />
<input type="checkbox" name="effect" value="MPSAP" />MP Sap<br />
<input type="checkbox" name="effect" value="StatSAP" />Stat Sap<br />
<input type="checkbox" name="effect" value="None" />None<br />
<input type="submit" value="Click Effect" />
<?php
$effect = $_POST['effect'];
$potion->effect = $effect;
if ($potion->effect == "poison")
{
	$poison = true;
}
else if ($potion->effect == "HPSAP")
{
	$HPSAP = true;
}
else if ($potion->effect == "MPSAP")
{
	$MPSAP = true;
}
else if ($potion->effect == "StatSAP")
{
	$statSAP = true;
}
else if ($potion->effect == "None")
{
	$statSAP = true;
}
if ($potion->name != " ")
{
	createPotion($player);
}



Could somebody explain why this is doing this and how to fix it? That would be loverly.

This post has been edited by RUFerret: 21 December 2010 - 09:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Fatal Error: Fatal error: Call to undefined function

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,151
  • Joined: 08-August 08

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 09:40 AM

That function is a method belonging to the potion class. Try:
    $potion->createPotion($player);


Was This Post Helpful? 1
  • +
  • -

#3 RUFerret  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 17-December 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 10:19 AM

Bah, how could I not see that...

Now the problem is when I click a status effect (like poison) and hit submit, nothing happens. By default, the function is echoing "has been created"

Are the variables being set properly?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,151
  • Joined: 08-August 08

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 10:44 AM

Look at what your method does. It says has been created no matter what!
Was This Post Helpful? 0
  • +
  • -

#5 RUFerret  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 17-December 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 10:59 AM

Well of course! But why?! That's what I can't figure out.

When a checkbox is clicked, it should return true. I don't think the values are being set properly to $effect.

Is it because there is no database connected that the createPotion() is executing all the time?

Because it shouldn't, since the potion has no name.
Was This Post Helpful? 0
  • +
  • -

#6 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 11:15 AM

In that big if-else block your not using the class variable
if ($potion->effect == "poison")  
{  
     $poison = true; // $potion->poison = true;  
}  



Same thing as your first problem.

Although I'm not sure why you just don't check the effect variable in your createPosion though.

This post has been edited by JaKWaC: 21 December 2010 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,151
  • Joined: 08-August 08

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 11:33 AM

Your code says:

if the potion name is not a space, then create a potion by calling the createPotion method. That method returns nothing. It simply does some if statements which set some local variables and then it outputs your message regardless of what happened before.
Was This Post Helpful? 0
  • +
  • -

#8 RUFerret  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 17-December 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 11:39 AM

Hmm, am I passing the object correctly?

When I submit a potion name, the only thing that changes is the "Add what to $potion->name" part. Otherwise nothing with the checkboxes are working, it's not echoing anything, which means $effect is not being set correctly.

Am I right in being wrong?
Was This Post Helpful? 0
  • +
  • -

#9 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 11:41 AM

See my post above:
http://www.dreaminco...0&#entry1201250
Was This Post Helpful? 0
  • +
  • -

#10 RUFerret  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 17-December 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 11:47 AM

Yes I know, and I fixed that. Still nothing shows.

Updated code to eliminate old issues

<?php
error_reporting (E_ALL ^ E_NOTICE);
class potion
{
	public $name;
	public $effect;
	public $HPSAP;
	public $MPSAP;
	public $poison;
	public $StatSAP;
	public function specs($tempname)
	{
		$this->effect = " ";
		$this->name = $tempname;
		$this->HPSAP = false;
		$this->MPSAP = false;
		$this->poison = false;
		$this->StatSAP = false;
	}
	public function createPotion($potion)
	{
		if ($player->HPSAP == true)
		{
			echo "<br />HP Sap added";
		}
		else if ($player->MPSAP == true)
		{
			echo "<br />MP Sap added";
		}
		else if ($player->poison == true)
		{
			echo "<br />poison added";
		}
		else if ($player->StatSAP == true)
		{
			echo "<br />Stat Sap added";
		}
		echo "<br />".$potion->name." has been created.";
	}
}
echo "Type Potion Name<br />";
?>
<form method ="post" action="iSystem.php" />
<input name="potion" />
<input type="submit" value="Create Potion" /><br /><br />
<?php
$potionname = $_POST['potion'];
$potion = new potion();
$potion->specs($potionname);
echo "Add what to ".$potion->name;
echo "<br /><br />";
?>
<form method ="post" action="iSystem.php" />
<input type="checkbox" name="effect" value="poison" />Poison<br >
<input type="checkbox" name="effect" value="HPSAP" />HP Sap<br />
<input type="checkbox" name="effect" value="MPSAP" />MP Sap<br />
<input type="checkbox" name="effect" value="StatSAP" />Stat Sap<br />
<input type="checkbox" name="effect" value="None" />None<br />
<input type="submit" value="Click Effect" />
<?php
$effect = $_POST['effect'];
$potion->effect = $effect;
if ($potion->effect == "poison")
{
	$potion->poison = true;
}
else if ($potion->effect == "HPSAP")
{
	$potion->HPSAP = true;
}
else if ($potion->effect == "MPSAP")
{
	$potion->MPSAP = true;
}
else if ($potion->effect == "StatSAP")
{
	$potion->statSAP = true;
}
else if ($potion->effect == "None")
{
	$potion->statSAP = true;
}
if ($potion->name !== " ")
{
	$potion->createPotion($player);
}


Was This Post Helpful? 0
  • +
  • -

#11 JaKWaC  Icon User is offline

  • D.I.C Head

Reputation: 76
  • View blog
  • Posts: 234
  • Joined: 15-November 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 11:49 AM

Okay, I see what your talking about now I believe. Your probably submitting the first form and expecting the database to be saved when you submit the next form. You can't do that, you either need to use a single form or save the class in a session.

Your also passing $player to your createPotion. I don't see a $player variable set anywhere.

Also in your createPotion function my bet is that you are going to want to use $this instead of $player and $potion.

This post has been edited by JaKWaC: 21 December 2010 - 11:54 AM

Was This Post Helpful? 1
  • +
  • -

#12 RUFerret  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 17-December 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 11:58 AM

I changed the for from iSystem.php to
<?php echo $_SERVER['PHP_SELF']; ?>


Would I need to connect this to a database for it to work or am I just missing something? I mean, the name is set, then the effect is set.

But then again, when you submit an effect, then name goes away..

I swear I'm retarded sometimes...

it was the player potion issue.. Thank you so much haha I can't believe I did that.. beginner mistake I claim!

Final correct code:

<?php
error_reporting (E_ALL ^ E_NOTICE);
class potion
{
	public $name;
	public $effect;
	public $HPSAP;
	public $MPSAP;
	public $poison;
	public $StatSAP;
	public function specs($tempname)
	{
		$this->effect = " ";
		$this->name = $tempname;
		$this->HPSAP = false;
		$this->MPSAP = false;
		$this->poison = false;
		$this->StatSAP = false;
	}
	public function createPotion($potion)
	{
		if ($potion->HPSAP == true)
		{
			echo "<br />HP Sap added";
		}
		else if ($potion->MPSAP == true)
		{
			echo "<br />MP Sap added";
		}
		else if ($potion->poison == true)
		{
			echo "<br />poison added";
		}
		else if ($potion->StatSAP == true)
		{
			echo "<br />Stat Sap added";
		}
		echo "<br />".$potion->name." has been created.";
	}
}
echo "Type Potion Name<br />";
?>
<form method ="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" />
<input name="potion" />
<input type="submit" value="Create Potion" /><br /><br />
<?php
$potionname = $_POST['potion'];
$potion = new potion();
$potion->specs($potionname);
echo "Add what to ".$potion->name;
echo "<br /><br />";
?>
<form method ="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" />
<input type="checkbox" name="effect" value="poison" />Poison<br >
<input type="checkbox" name="effect" value="HPSAP" />HP Sap<br />
<input type="checkbox" name="effect" value="MPSAP" />MP Sap<br />
<input type="checkbox" name="effect" value="StatSAP" />Stat Sap<br />
<input type="checkbox" name="effect" value="None" />None<br />
<input type="submit" value="Click Effect" />
<?php
$effect = $_POST['effect'];
$potion->effect = $effect;
if ($potion->effect == "poison")
{
	$potion->poison = true;
}
else if ($potion->effect == "HPSAP")
{
	$potion->HPSAP = true;
}
else if ($potion->effect == "MPSAP")
{
	$potion->MPSAP = true;
}
else if ($potion->effect == "StatSAP")
{
	$potion->statSAP = true;
}
else if ($potion->effect == "None")
{
	$potion->statSAP = true;
}
if ($potion->name !== " ")
{
	$potion->createPotion($potion);
}
?>



Thanks again!
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 12:11 PM

your potion class has a design flaw.
- what the createPotion() method does is not logically related to the potion class. this way using a function would make more sense.
- I consider line #85 stupid. why passing the same object to its method? using $this in the method would be appropriate.
- Id rename createPotion() to analysePotion() (thats what the method does)
- currently theres no need for the specs() method when you give the class members a default value.
Was This Post Helpful? 1
  • +
  • -

#14 RUFerret  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 17-December 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 12:22 PM

When I take out the parameters, the function fails to execute correctly
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3525
  • View blog
  • Posts: 10,171
  • Joined: 08-June 10

Re: Fatal Error: Fatal error: Call to undefined function

Posted 21 December 2010 - 12:53 PM

obviously. did you replace $potion by $this?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2