Forum Moderators: coopster

Message Too Old, No Replies

To kill a function

recurring function declarations

         

Drunk N Japan

9:16 pm on Aug 6, 2007 (gmt 0)

10+ Year Member



I have a cron job I amm writing that includes recursively several similar php scripts to parse xml. The problem I am having is that all the scripts have identically named functions. how can I kill the functions after each include?

ramoneguru

12:06 am on Aug 7, 2007 (gmt 0)

10+ Year Member



Hmmmm, well as you already found out the function (along with all its code) is pushed onto the stack and isn't popped until the function is executed......

Question.
So does it look like this:
File1.php has a function labeled getSomething()
File2.php has a function labeled getSomething()
File3.php has a function labeled getSomething()
File4.php has a function labeled getSomething()

and your cron job is trying to distinguish between getSomething() from File1.php and getSomething() in File2.php?

You know, maybe renaming it is out of the question but, a simple search and replace to rename them would be like "go to each line, find the keyword 'function' and replace it with 'function file1'" for the file1.php functions and similar for file2.php, etc... Only problem is then calling you'll need to distinguish which ones you want called. Iono, I'm just tossing ideas out there :P

--Nick

Habtom

5:10 am on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could:
> Rename the functions
> Put them in a class with different class names

Does anyone see difficulties on the second option?

Habtom

joelgreen

10:30 am on Aug 7, 2007 (gmt 0)

10+ Year Member



Are these functions doing same thing? If so, maybe this would help [ua.php.net...]

vincevincevince

10:35 am on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't include them... use file_get_contents() with the complete path:
file_get_contents("http://example.com/scripts/script1.php");
file_get_contents("http://example.com/scripts/script2.php");
file_get_contents("http://example.com/scripts/script3.php");

That will execute the scripts as their own separate threads. If you need the response from the scripts, that's the return value of file_get_contents:

$r1=file_get_contents("http://example.com/scripts/script1.php");
$r2=file_get_contents("http://example.com/scripts/script2.php");
etc.

Drunk N Japan

2:21 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



Thanks for all the ideas. I am thinking about using a variable.
$n=1;
include ($script);
$n +=1;

Then incrementing this variable as each script is run. Then in the scripts adding the $n to the function name so that each function has a unique name. Unfortunately this will mean there is a possibility of somewhere in the range of 225 functions being open at the same time.

I do like the idea of using

file_get_contents($script);

which I tried but it doesn't seem to execute the script like an include does.

Yes unfortunately all the scripts have the same function names in them and they are all named the same in every script.

I didn't write the scripts I just have to use them as they were provided by a vendor. The scripts are for reading in xml and writing it to a MySQL database. this probably would explain the repetitiveness. Let me know if you have any more ideas.

especially something along the lines of the

file_get_contents($script);

I like the idea of not having to worry about what functions are called in each script. I have also wondered if
runkit_function_remove(string $functionName);

would work to kill a function after execution.

Thanks to all of ya'll for your advice and guidance.

vincevincevince

2:33 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do like the idea of using
file_get_contents($script);

which I tried but it doesn't seem to execute the script like an include does.


Make sure you use the full HTTP path (http://....) not a server path or relative path.

And you may be right - it is a different way that they are called - I was assuming they were essentially standalone scripts which you needed to 'execute' rather than includes which would be providing data or functions back to your main file.

Drunk N Japan

2:49 pm on Aug 7, 2007 (gmt 0)

10+ Year Member




Make sure you use the full HTTP path (http://....) not a server path or relative path.

And you may be right - it is a different way that they are called - I was assuming they were essentially standalone scripts which you needed to 'execute' rather than includes which would be providing data or functions back to your main file.

They are each standalone scripts which I need to execute. But doesn't file_get_contents return the file in a string?
I may be a little confused here.
If it returns the contents of the file in a string then do I need to execute the string?

Let me make this a little easier I will post the cron script.


<?php
//set the file to read and move
$today = date("m-d-y");

//connect to the database
dbx_connect();
$result = mysql_query("SELECT * FROM stats_files") or die(mysql_error());
while($row = mysql_fetch_array($result)){

//set the variable using the database rows
$filename = $row['filename'];
$folder = $row['folder'];
$script = $row['script'];
$old_file = "$filename.xml";
$new_file = "$folder/$filename/$filename_$today.XML";

// check to see if this particular file has been delivered tonight.
if (file_exists($old_file)) {

//file found
echo "I found the file $filename <br/>";

//execute the appropriate script
file_get_contents($script);
//include($script);

//move file to appropriate folder add date to name
//not working due to phpini permissions
//tried the ftp alternative and cried "No Joy"
//chown($old_file, nobody);
//chmod($old_file, 0777);
//rename($old_file, $new_file);

}
else {echo "File $filename is not available <br/>";}
}

//the only global function(I hope)
function dbx_connect() {
mysql_connect("********","********_********","********");
mysql_select_db("********_test");
}
?>

vincevincevince

3:02 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the path given to file_get_contents() is a local or server path (e.g. ../something.php or /var/www/domain.com/something.php) then it will just return the contents of the file. True.

However, if you pass a complete URL (not path) i.e. starting with [......] then it will actually execute it and what is returned is the result of executing the script.

Drunk N Japan

3:42 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



"Hallelujah...", "Hallelujah...","Hallelujah...".

How wickedly awesome is that. I got it to work. I took and created a variable called $ran and made it a blank variable before the execution of the script. Then I ran the script equal to the variable. Last I echoed the output so I could see it.

//file found
echo "I found the file $filename <br/>";
$ran = "";
//execute the appropriate script
echo "$script <br/>";
$ran=file_get_contents($script);
//to see the output of the script
echo "$ran <br/>";
echo "$n <br/>";
$n += 1;

$n is just being used as a counter for the time being to keep track of things.

vincevincevince you are da man. Thank you also to all the other responses. I kind of used all of them to find the last solution.

One last question....
Can * be used as a wildcard character when you don't know the exact name of a particular file you want to access?

example: $file = "../my_STORY*.xml";
where the file name is always different after STORY.
the file name changes with a date stamp my_STORY$20070517213819080000201.xml

Thanks,

vincevincevince

4:16 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can * be used as a wildcard character when you don't know the exact name of a particular file you want to access?

Not with file_get_contents(), but you can use directory opening code. Go and read up on opendir [php.net], especially the comments at the bottom - you will find examples there of how to read through a list of files in a directory - it should be easy enough to add something to find something from that list which matches your pattern and then use that

Drunk N Japan

6:01 pm on Aug 7, 2007 (gmt 0)

10+ Year Member



"vincevincevince..." "vincevincevince..." "vincevincevince..."

I used the opendir to list all the files in the directory, then I used a preg_match to find files that started with the exact wording that I wanted to use. After I found the files I then went ahead and ran the script for that particular file.

"Genius." "Genius I tell you."

Here is the part I added to find the files that I wanted.


//finding and running the stories as they have a dynamic name
if ($filename == "my_STORY"){
$path = "path/to/the/directory/that/holds/the/xml";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh))!== false) {
if($file!= "." && $file!= "..") {

//get only the files I want
if (preg_match("/my_STORY*/",$file)){
$filename = $file;
echo "$filename Was Found! <br/>";
$ran = "";

//execute the appropriate script
echo "$script <br/>";
$ran=file_get_contents("$script?file=$filename");

//to see the output of the script
echo "$ran <br/>";
echo "$n <br/>";
$n++;
}

//to print out the contents of the whole directory
// echo "$i. <a href='$path/$file'>$file</a><br />";
$i++;
}
}
closedir($dh);
}

since I receive about 10 stories a night with these types of file names I had to do this.
my_STORY$20070517213819080000201.xml
my_STORY$20070517224752740000101.xml
my_STORY$20070518225828980000101.xml
my_STORY$20070518233807270000101.xml

I hope this can help someone else trying to do this type of thing.

Drunk

vincevincevince

1:46 am on Aug 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to be of help.

Note that your /my_STORY*/ preg_match isn't working as you think - what I think you mean is /^my_STORY.*$/