Status
Not open for further replies.

Robin H

Active Member
807
2009
73
0
PHP:
[Help]Redeclare A function[/b]

Right now file-remoter is having a shitload of functions.

If I want to generate rapidshare remoteable links I called the function:

$links .= gen_rs_links($links);

At first I didn't see any harm. The code is cluttered but it's still readable.
But now I'm supporting 11 hosts.

Each host has it's own functions ( main function, grab cookies, check links )

I was rewriting it so I could just do a
[PHP]
foreach($dlinks as $host => $urls){
  $links .= generate_links($host,$urls);
}
the function generate_links would be:

PHP:
function generate_links($host,$links){
  include('/path/inc/'.$host.'.php');
  return link_gen($links);
}
The function link_gen would be unique for every host.

But apparantly, even in a function. The scope of functions included using the include function is global.

So I figure that if I have to generate links for multiple hosts, an error will come because I include a function that's already been declared before.

Any ideas?
 
6 comments
RaJ, the functions work fine.

I just need to figure out a way to re-declare functions.
.. I'll google -_-

edit:
Finished googling. Not happy with the results I was getting :(
 
To be honest you want to place all your methods in a Namespace and then there will be no conflict.

PHP:
namespace Functions
{
    function file_get_contents($path)
    {
        return \file_get_contents($path); //the \ is to resolve at the root scope
    }
}


then use like so:

PHP:
$data = Functions\file_get_contents($path);

This should resolve scope and declaration issues.
 
Have you tried include_once instead of include? It will check if it has been included and not try to include it a second time.

The only problem with using something like namespace is it limits your code to php 5 servers and up.
 
Nah,
I'm just going to fix it with using the API

PHP:
foreach($dlinks as $host => $urls){
  $links .= generate_links($host,$urls);
}
Would become
PHP:
foreach($dlinks as $host => $urls){
  $post = array();
  $post['links'] = implode("\n",$urls);
  $post['userID'] = $userID;
  $post['host'] = $host;
  $links .= getpage('http://api.file-remoter.com/?act=generate',$post);
}

I know it's not good practice. But fuck it
 
Status
Not open for further replies.
Back
Top