Call me stupid, I dont mind ! (php related)

Status
Not open for further replies.

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
I am creating a series of libraries and have run into a few major issues.

The problems are as follows :
when I include a file that only holds a few variables and then include it again by calling include_once, it doesn't redeclare them.

The biggest problem I am having is I have 2 scripts that contain almost the same function, it is declared by the same name.
A mt_rand() function chooses between the 2, but the catch is on some runs of the script it runs both functions and dies with a fatal error : cannot redeclare function ...
Is there a way of undeclaring a function and then redeclaring it.

Another one, when a script is required or required_once php reads in the contents right. So if i where to call the same function from that script twice. Does the function stay as is and run as expected ?
 

bonzo meier

Member
Messages
47
Reaction score
0
Points
6
I include a file that only holds a few variables and then include it again by calling include_once, it doesn't redeclare them

I´m not sure if I get this right, but anyway: have you tried unsetting the variables before including them again? like

Code:
<?php
include_once (myincludedvariables.php);

workwithvariables($x, $y);

unset $x; unset $y;
include_once (myincludedvariables.php);

if you changed your variables in the workwithvariables()-function, you'll get the original value back. is that what you intended?

Is there a way of undeclaring a function and then redeclaring it.
I don´t know. But if the two functions are almost the same, it means they are different by some mean. why not giving them different names and let the mt_rand() function decide which one to pick?
 

DefecTalisman

Community Advocate
Community Support
Messages
4,148
Reaction score
5
Points
38
Tried using :
PHP:
<?php
unset($var,$another_var);
?>
no avail, looking at only declaring them once and using :
PHP:
<?php
$var['id'] = 'something';
$var['blob'] = 'another thing';

global $var;
?>

With the functions, the script requires that they are named the same for the ease of adding more files that contain 2 functions each, but all scripts declare and return the same.
example:
PHP:
<?php

$r = mt_rand(1,2);
require 'file_'.$r.'.php';
unset($r);

$test= 1st_func($var,$another_var);

... // do something with $test;

$untest = 2nd_func($var,$another_var,$test);


$r = mt_rand(1,2);
require 'file_'.$r.'.php';
unset($r);

$test2= 1st_func($var,$another_var);

... // do something with $test2;

$untest2 = 2nd_func($var,$another_var,$test2);

?>

I have used a rather nasty way to get around this for now by using separate files for each function and naming them differently. Then just including them with a similar random method:
 
Last edited:
Status
Not open for further replies.
Top