colombian Posted June 25, 2008 Share Posted June 25, 2008 I am stuck on something terribly simple, but Google isn't helping. How do I call a function inside another function in PHP5? It says the function is undefined. <?php function a() { echo "a"; } function b() { a(); } ?> When calling function b it says a is undefined. I was sure that all function scope was global. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 25, 2008 Share Posted June 25, 2008 The posted code works. Post your actual error message and the complete code that causes the error. Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/#findComment-573711 Share on other sites More sharing options...
bluejay002 Posted June 25, 2008 Share Posted June 25, 2008 just a little point: when naming functions, make it meaningful, a() and b() does not look so healthy. I dont get what is wrong with your code but you can try these: <?php function displayText() { echo "I am calling a function!"; } function callDisplay() { displayText(); } callDisplay(); ?> Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/#findComment-573713 Share on other sites More sharing options...
colombian Posted June 25, 2008 Author Share Posted June 25, 2008 Well good to know, I thought I was going crazy. The function is basically that - however, it's inside a CAKEPHP application. I am just starting to get familiar with CAKEPHP, so there must be something going on that's preventing it from happening. I just tested it outside CAKE and it does work fine... If you know cake... It's inside a controller, I need two very similar functions, so I am using one function and passing the 1 parameter to both. The error is the normal: "Fatal error: call to undefined function....." and it specifies the line number where I am calling function a inside function b. No wonder why google wasn't of help, I'll go delve into CAKE - thanks again, Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/#findComment-573720 Share on other sites More sharing options...
bluejay002 Posted June 25, 2008 Share Posted June 25, 2008 i see... its been a while since the last time I had my cake baking session. anyway, you might want to go to this forum, this is a cakePHP forum: http://www.cakephpforum.net/ Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/#findComment-573735 Share on other sites More sharing options...
hitman6003 Posted June 25, 2008 Share Posted June 25, 2008 I was sure that all function scope was global. No, a function's scope is limited to where it was declared at. If it is declared in the global namespace, then, yes, it is global. However, if it is declared in a class, it it not global, but local to the class. If cakePHP is anything like Zend, and assuming you are using your posted code in a view script, then it is probably evaluated code inside the view class...try calling the function using the $this-> nomenclature.... <?php function a() { echo "a"; } function b() { $this->a(); } ?> The worst that will happen is nothing. Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/#findComment-573742 Share on other sites More sharing options...
colombian Posted June 25, 2008 Author Share Posted June 25, 2008 It is inside the class, and it is being used in that class, but somehow it is still complaining. I'll post the answer here at some point in the next few days. The real function names are descriptive - I've been doing PHP for a while, it's CAKE that was tripping me. Thanks for the responses. Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/#findComment-573858 Share on other sites More sharing options...
DoddsAntS Posted June 25, 2008 Share Posted June 25, 2008 I don't use cake so this may be way off the mark, but it may also work for you. Depending on how you are calling function b will depends how you need to define it in the class file. if your calling it in like $objectName = new object(); $objectName->b(); then hitman6003s response should work fine, however if you call the function like object::b(); you will need to define the function as follows function a() { echo "a"; } function b() { self::a(); } hope this helps Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/#findComment-573943 Share on other sites More sharing options...
hitman6003 Posted June 25, 2008 Share Posted June 25, 2008 however if you call the function like object::b(); you will need to define the function as follows function a() { echo "a"; } function b() { self::a(); } The function should (would have to?) be declared static as well. Link to comment https://forums.phpfreaks.com/topic/111757-simple-function-cal-inside-function/#findComment-574028 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.