birdzhavefangs Posted May 28, 2010 Share Posted May 28, 2010 So I'm having trouble getting a larger project to work- namely calling functions (from an outside class) within functions. I'm sure this has been asked before, but I can't really think of a way to phrase it well enough for searching, so I apologize! Here's my example: First off is bob.php, the class file <?php class Bob { function loud( ) { echo "Bob\n"; } function greet($name) { echo "Hello, ".$name."!\n"; } } ?> And second is bobuser.php, that requires bob: <?php require_once("bob.php"); $B = new Bob; $B->loud(); function bob2(){ $B->loud(); } bob2(); echo "oh noes"; ?> The output I want is: Bob Bob oh noes! Versus the output I'm getting is: Bob I'm working on large project right now, and I don't want to post it's code (due to mess, and laaarge size), hence the very simple example. Thank you for your attention! Link to comment https://forums.phpfreaks.com/topic/203242-calling-functions-from-external-class-within-functions/ Share on other sites More sharing options...
trq Posted May 29, 2010 Share Posted May 29, 2010 $B does not exist within your bob2 function, you will need to pass it in as an argument. <?php require_once("bob.php"); $B = new Bob; $B->loud(); function bob2(Bob $obj) { $obj->loud(); } bob2($B); echo "oh noes"; ?> Link to comment https://forums.phpfreaks.com/topic/203242-calling-functions-from-external-class-within-functions/#findComment-1064885 Share on other sites More sharing options...
birdzhavefangs Posted May 29, 2010 Author Share Posted May 29, 2010 Thanks thorpe! I got it to work! Although, declaring with ($obj) instead of (Bob $obj) function bob2($obj) { $obj->loud(); } Link to comment https://forums.phpfreaks.com/topic/203242-calling-functions-from-external-class-within-functions/#findComment-1064888 Share on other sites More sharing options...
trq Posted May 29, 2010 Share Posted May 29, 2010 function bob2(Bob $obj) { would force $obj to be of type Bob which is in this case what you want. Link to comment https://forums.phpfreaks.com/topic/203242-calling-functions-from-external-class-within-functions/#findComment-1064890 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.