Jump to content

Recommended Posts

 

Fatal error: Cannot redeclare build() (previously declared in /build/a_builder.php:17) in /build/s_builder.php on line 188

 

The issue is that they both have build() functions.

 

And I am using

 

for{

   

    // other code

 

    include_once("/build/$buildFile");

    $result = build($pId,$Data);

 

 

}

 

I am suppose to call build() functions for one device and then for the other and then for third type of device and so firth.

 

It goes fine for the first time for i = 0

 

but for i=1 .. it starts giving me this error Fatal error: Cannot redeclare build() (previously declared in /build/a_builder.php:17) in /build/s_builder.php on line 188

 

Link to comment
https://forums.phpfreaks.com/topic/250662-include_once-error/
Share on other sites

You can't redeclare a function - doesn't matter where it came from.

 

There's another technique you can use that makes use of the fact that include()d files can return values.

Turn what you have now

// your code
for {
include_once("/build/$buildFile");
$result = build($pId, $Data);
}

// those other files

function build($pId, $Data) {
// do whatever
return $value;
}

?>

into

// your code
for {
// $pId and $Data will be used by the file
$result = include("/build/$buildFile");
}

// those other files

// do whatever
return $value;

?>

Link to comment
https://forums.phpfreaks.com/topic/250662-include_once-error/#findComment-1286073
Share on other sites

You can't redeclare a function - doesn't matter where it came from.

 

There's another technique you can use that makes use of the fact that include()d files can return values.

Turn what you have now

// your code
for {
include_once("/build/$buildFile");
$result = build($pId, $Data);
}

// those other files
<?php

function build($pId, $Data) {
// do whatever
return $value;
}

?>

into

// your code
for {
// $pId and $Data will be used by the file
$result = include("/build/$buildFile");
}

// those other files
<?php

// do whatever
return $value;

?>

 

 

Thanks requinix for such a quick reply..

 

the problem in doing that is that

 

1st - there are around 40-50 build files, that I will have to change

2nd - Its a huge project and if I make a change to all those build files , then I will have to make your mentioned change to all the other places where the build files are being used

 

 

Is there any other wayaround..

 

 

 

I have tried using the classes.

 

 

<?php

 

// other things

 

for {

 

    // code

 

  $tempobj = new buildclass();

    $result=$tempobj->func();

 

 

}

 

?>

 

 

 

<?php

class buildclass {

 

public func(){

 

      include_once("/build/$buildFile");

$result = build($pId, $Data);

 

      }

}

?>

 

I thought since a new object is created every time , hence it will work .. but apparently it doesn't .. it is still giving me the same error ..

Link to comment
https://forums.phpfreaks.com/topic/250662-include_once-error/#findComment-1286077
Share on other sites

Is the logic inside each of these build() functions the same, with only different data values or is the logic completely different?

 

If the build() function logic is different in each file, unless you use namespaces, you are going to need to uniquely name each function.

 

To get help with what you are doing, you are going to need to post at least two examples of the build() functions you are including and I can just about guarantee that you are going to need to change the structure of the code no matter what you do to solve this.

Link to comment
https://forums.phpfreaks.com/topic/250662-include_once-error/#findComment-1286086
Share on other sites

lets take that I have 3 build functions(completely different just same name) in 3 different files ..  (in actual there are around 40-50)

 

a_build.php 

 

build(){

 

// abcd

 

 

}

 

 

b_build.php

 

build(){

 

 

// 1234

 

 

}

 

 

c_build.php

 

build(){

 

 

// wxyz

 

 

}

 

---------------------------------------------------------------------------------------------

 

and I am supposed to call the build() depending upon which type it is ..

 

here are the 2  files that I created

 

 

builder.php    // it is supposed to call all the build() functions one by one depending upon the types

                                                        // Its a CRON JOB

 

// get the list

 

foreach(item in the list){

 

include_once("/helper/build_helper.php")

$result= build_the_device($pId,$data)

 

 

}

 

.............................................................................................

 

build_helper.php

 

function build_the_device($pId,$Data){

 

    $buildFile=CssUtil::GetBuildFileName($devModel, $devVendor);      // $buildFile gets the name of the file like a_build.php

    include_once("/build/$buildFile");                                            // I include that file

    $result = buildDevice($pId,$Data);                            // and I call the respective build() func

 

  }

}

 

 

 

 

Also , can I use exec command anyway .. or threading ..

 

 

I really appreciate all your help .. I am really new to php ..

Link to comment
https://forums.phpfreaks.com/topic/250662-include_once-error/#findComment-1286231
Share on other sites

As already stated, you can only define a function name once. I'm going to guess that you also have other function names that are redefined between these included files, but you are only seeing the error for the build() function because it is a fatal runtime error and the code never gets around to redefining the other functions in the files. If you must include more than one of your files at the same time, your current scheme won't work (there's no way to un-define a function and functions have global scope) and your code will need to be rewritten to either use unique names for the functions or use unique class names (you already have unique file names with unique code in each file) or do what requinix suggested and eliminate the build() function definitions.

 

However, based on the minimal information you actually provided ($devModel, $devVendor), your system definition is flawed. You should be manipulating different sets of data using one set of code, not a different set of code for each model/vendor or if your code/variables in each file are really unique, separate, and useful, you should be using classes (with unique names) instead of functions/variables.

 

You better start modifying those files if you have 40-50 of them to do or since you will need to change what you are doing to get it to work at all, you could post an actual example of what you are doing and someone might suggest a workable system to use.

 

Didn't someone prototype a test case and try this scheme before forging ahead with making an entire untested and unworkable system of code?

Link to comment
https://forums.phpfreaks.com/topic/250662-include_once-error/#findComment-1286263
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.