Jump to content

Simple function cal inside function


colombian

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.