[SOLVED] Calling a function within a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

[SOLVED] Calling a function within a function

Post by mudvein »

I have a small problem. I'm trying to call a function from within another function.

Code: Select all

<?php
class MultiMan
{
  function MultiFile()
  {
     MyOtherFunction();
  }
  function MyOtherFunction()
  {
    echo 'it works';
  }
}
?>
But when I do this, it says Fatal error: Call to undefined function: MyOtherFunction()

can anyone help?
Last edited by mudvein on Sun Mar 27, 2005 6:51 pm, edited 1 time in total.
traherom
Forum Newbie
Posts: 13
Joined: Tue Mar 15, 2005 6:43 am

this-> type thing

Post by traherom »

Try putting $this-> in front of the call to MyOtherFunction().
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

didn't work, but that's for variables anyways not functions lol...


anyways, i fixed the problem ( was a problem in another file), but now i have a NEW problem!

the file that i'm calling my class with has this (and this is all the lines there are)

Code: Select all

?php
$a = new MyClass;
for($i=0; $i<7; $i++)
{
	$a->myFunction('variables'.$i,'2','2','3');
}
?>
when i try to run this, it gives me the following error :
Fatal error: Cannot instantiate non-existent class: MyClass in D:\apache\Apache2\htdocs\betafolder\testme.php on line 2


I thought I could initiate a NEW statement and it would initiate the correct class file for me automatically? Is there a setting i need to change in order to make this possible without me having to use an include('myclassfilenamehere.php'); ???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$this is for all member variables and methods of the current instance of the class.

php5 has autoload, for php4, you must include() in some fashion.
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

yes i'm using php4... thanks
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

so you are saying that if i need to call a function from within another function, i must call that new function by using $this-> ???? such as my example above (but with $this-> added to the function's function call?)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

&lt;?php
class MultiMan
{
  function MultiFile()
  {
     $this-&gt;MyOtherFunction();
  }
  function MyOtherFunction()
  {
    echo 'it works';
  }
}

$foo =&amp; new MultiMan();

$foo-&gt;MultiFile();
?&gt;
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Oh nopzz! (that means no)

Here's how it works.

Code: Select all

class funkyClass
{
  function bang()
  {
    echo "Bang in the Class!";
  }
}

function bang()
{
  echo "Bang in the globe!";
}
First of all, these two "identically named" functions won't clobber each other because one is inside the class, and the other isn't. Let's instantiate our class.

Code: Select all

$instance = new funkyClass;

$instance->bang();
bang();
The first call to the function will return a Bang from inside the class, while the second will return "Bang" from the globe.

Code: Select all

function slurp() {
  echo "SLURP!";
}

class boomBang
{
  function bang()
  {
    echo "BANG!";
  }
  
  function boom()
  {
    echo "BOOM!";
    $this->bang();
    slurp();
  }
}
Note what I had to do to call each function in this last example. Any questions?
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

nope that made it perfectly clear :D thanks.
Post Reply