OOP, calling methods from within methods

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
User avatar
fendtele83
Forum Commoner
Posts: 27
Joined: Tue Oct 25, 2005 2:21 pm
Location: Woodbridge, NJ

OOP, calling methods from within methods

Post by fendtele83 »

how do i do it in a php4 compatible environment... this is what im trying to do:

Code: Select all

class say_hi {

function print_hi()
	{
		echo "hi";
	}
function call_hi()
	{
		print_hi();
	}
}

$sayHi = new say_hi();
$sayHi->call_hi();
i get this error: Fatal error: Call to undefined function: print_hi() in say_hi.class.php on line 151

what's the deal????
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

class say_hi {

function print_hi()
   {
      echo "hi";
   }
function call_hi()
   {
      $this->print_hi();
   }
}

$sayHi = new say_hi();
$sayHi->call_hi();


Moved to PHP - Code.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

you have to let it know where the function is...

Code: Select all

function call_hi() 
   { 
      $this->print_hi(); 
   }
User avatar
fendtele83
Forum Commoner
Posts: 27
Joined: Tue Oct 25, 2005 2:21 pm
Location: Woodbridge, NJ

Post by fendtele83 »

duuuuhhhhhh.... haha, thanks...

that was actually the first thing i tried... but i used a search and replace and actually replaced the function name with $this->functionName as well... that's why i didnt get it to work...
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

for static calling (iirc):

Code: Select all

class say_hi {

function print_hi()
   {
      echo "hi";
   }
function call_hi()
   {
      self::print_hi();
   }
}


say_hi::call_hi();
Post Reply