Page 1 of 1

[SOLVED] Calling a function within a function

Posted: Sun Mar 27, 2005 6:17 am
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?

this-> type thing

Posted: Sun Mar 27, 2005 6:35 am
by traherom
Try putting $this-> in front of the call to MyOtherFunction().

Posted: Sun Mar 27, 2005 1:28 pm
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'); ???

Posted: Sun Mar 27, 2005 1:40 pm
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.

Posted: Sun Mar 27, 2005 1:43 pm
by mudvein
yes i'm using php4... thanks

Posted: Sun Mar 27, 2005 1:58 pm
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?)

Posted: Sun Mar 27, 2005 2:08 pm
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;

Posted: Sun Mar 27, 2005 2:13 pm
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?

Posted: Sun Mar 27, 2005 2:44 pm
by mudvein
nope that made it perfectly clear :D thanks.