Welcome Guest, Not a member yet? Register   Sign In
private function = Call to undefined function error
#1

[eluser]stef25[/eluser]
ive got some processing code in my controller id like to break out in to a separate function.

the function in question is at the bottom and its called from

Code:
$existing_tags = array_flatten($existing_tags);

Code:
function array_flatten($array)
    {
        $ab = array(); if(!is_array($a)) return $ab;
        foreach($a as $value){
            if(is_array($value)){
                $ab = array_merge($ab,array_flatten($value));
            }else{
                array_push($ab,$value);
            }
        }
        return $ab;
    }

this returns a "Call to undefined function array_flatten()" even though both snippets are in the same controller file.

i dont want this function to be accessible from the url but i removed the _underscore just to debug.

this is basic php but im stumped. anyone?
#2

[eluser]Fatih[/eluser]
If you use PHP5, you can add "private" before function.
#3

[eluser]stef25[/eluser]
i do use PHP5 and had already tried that, no luck ...
#4

[eluser]Dam1an[/eluser]
try
Code:
$existing_tags = $this->array_flatten($existing_tags);
#5

[eluser]slowgary[/eluser]
Don't you need to use this?

As in...
Code:
$existing_tags = $this->flatten_array($existing_tags);

Edit: You beat me to it Dam1an. Because we're friends, I will forgive you, but just this once.
#6

[eluser]stef25[/eluser]
d'oh, thanks Dam1an and slowgary!
#7

[eluser]Dam1an[/eluser]
[quote author="slowgary" date="1248045629"]
Edit: You beat me to it Dam1an. Because we're friends, I will forgive you, but just this once.[/quote]

DOes that mean next time I beat you to it we won;t be friends, and you therefore won;t be able to forgive me? Tongue
#8

[eluser]slowgary[/eluser]
Next time I will ban your IP from the internets. All of them.
#9

[eluser]stef25[/eluser]
ill be shooting off some more questions over the next few weeks should you guys wonna go up against each other again, no probs for me :-D
#10

[eluser]slowgary[/eluser]
BTW stef25... in case you didn't already know this, prefixing your function names with an underscore prevents them from being run through the url in CodeIgniter. So currently someone could access your function by going to http://www.yourdomain.com/controller/array_flatten. While it won't do much but waste CPU cycles and possibly throw an error (wrong number of parameters or something), it would probably be more appropriate if that URL presented users with a 404. You wouldn't want the Google to get ahold of that URL, would you?

So...
Code:
function _flatten_array($array)
{
}
and...
Code:
$existing_tags = $this->_flatten_array($existing_tags);

If you're going to name your function "array_flatten()", you should for consistency sake, change the name of the function in your model to "all_tags_get()" - but sense that would not make, would it Master Yoda? ;-P




Theme © iAndrew 2016 - Forum software by © MyBB