...

View Full Version : is there workaround to use same function



ajloun
09-04-2009, 01:42 AM
Hello

is there workaround to use same function in same file.
i have One file Loads in the Top and the Center In my site , but get error

Fatal error: Cannot redeclare message() (previously declared in

Renaming the function won't help .. since the php file placed twice in my site and Should not be Renamed i need to have the file in same name and placed twice in my site..

Zangeel
09-04-2009, 01:49 AM
This just means, you've already created a function with the same name. If it does the same functionality, then you don't need to recreate it, however if you want a function with the same name but does something different, this is impossible, just rename it something new.

Fou-Lu
09-04-2009, 02:09 AM
This just means, you've already created a function with the same name. If it does the same functionality, then you don't need to recreate it, however if you want a function with the same name but does something different, this is impossible, just rename it something new.

Thats right, PHP does not support method or function overloading.
Chances are its a confliction of you're parameters, but with a similar functionality. What you can do is write a user function with an undetermined number of parameters until runtime.



function myFunc()
{
$argv = func_get_args();
$argc = func_num_args();
switch ($argc)
{
case 0:
// Do something when given no parameters
break;
case 1:
// Do something with just the one parameter
break;
default:
// Do something when given any other number of parameters.
}
}


Man I wish PHP supported overloading...

Now, this being all said, you can actually do overloading (in functions, not methods) as of PHP5.3.0 using namespaces:


<?php

namespace MyProject\FuncLoads1
{
function doFunc(array $a)
{
printf("\$a = %s\n", print_r($a, true));
}
}

namespace MyProject\FuncLoads2
{
function doFunc(\User $u)
{
printf("\$u = %s\n", $u);
}
}

namespace
{
class User
{
public function __construct()
{
$this->sUsername = 'Fou-Lu';
}
public function __toString()
{
$members = '';
$i = 0;
foreach ($this AS $key => $val)
{
if ($i++ > 0)
{
$members .= ', ';
}
$members .= sprintf("%s=%s", $key, $val);
}
return sprintf("%s[%s]\n", get_class($this), $members);
}
}

$a = array('a', 'b', 'c');
$u = new User();
MyProject\FuncLoads1\doFunc($a);
MyProject\FuncLoads2\doFunc($u);
}


I do not recommend combining multiple namespaces in a single file; however, the same logic applies on included files so you can redeclare functions.

ajloun
09-04-2009, 04:03 AM
Fou-Lu .. Thanks Man , you always there for Me and you Too Zangeel.

The Thing is i have Bad word Censor File , and i have One Block Calls the Censor file , the problem this File Placed in two Places in the Same Page . Thats why i get the Error , Because the Function was already Called By the File .

Fou-Lu i did not get you right .. Can you Make the Change you Mention Direct to this File


function SmartCensor($content, $Badwordfilter = 1)
{
if ($Badwordfilter == 0)
{
return $content;
}
else
{
$words_list=@file(XOOPS_ROOT_PATH.'/modules/ehdaa/censorwords.txt');
$search=array('a','b','i','l','o','p','s');
$replace=array('(?:a|\@|\*)','(?:b|8|3|\*)','(?:i|1|l|\!|\*)','(?:l|1|i|\!|\*)','(?:o|0|\*)','(?:p|\ ?|\*)','(?:s|\$|\*)');
foreach($words_list as $badword){
$badword=rtrim($badword);

$len=strlen($badword);
if($len <= 0)
continue;
$rep='';
for($i=0; $i < $len; $i++){
$rep.='*';
}

// Check to see if this word has brackets around it. If it is bracked, censor it in a simple form.
if (substr($badword, 0, 1) == "[" && substr($badword, ($len - 1), 1) == "]") {
$content = preg_replace("@".substr($badword, 1, ($len - 2))."@i", substr($rep, 0, (strlen($rep) - 2)), $content);
continue;
}

$badwordpreg=preg_split('//', $badword, -1, PREG_SPLIT_NO_EMPTY);
$badwordpreg=str_replace($search, $replace, $badwordpreg);
$badword='';
for($i=0; $i < count($badwordpreg); $i++){
$badword.=$badwordpreg[$i];
if($i != (count($badwordpreg)-1)) $badword.='(.{0,5})';
}
$badword="/$badword/i";
$content=preg_replace($badword, $rep, $content);
}
return $content;
}
}

Fou-Lu
09-04-2009, 06:45 AM
Is it the exact same definition? If so, you don't need a second one, you can just delete it. A function's purpose is to eliminate redundant code by allowing reuse.

ajloun
09-04-2009, 07:14 AM
The Above Code is the badwords.php file , that file called by another file lets call it comment.php .

Now comment.php is loaded in the Top of my site and in the Bottem .. So wht Happens since the file Placed twice in same Page , and then Calls the badwords.php .. this when the fatal error's happens because the Function called twice from the same file , . so i need workaround way ..

i thought somthing like this example ..
if (!function_exists("validate"))
{

$check = true;
}
else
{
$check = false;
}
return $check;
}
}

i just Coudt do it ..

Fou-Lu
09-04-2009, 09:18 AM
I'm guessing that the problem is you're using either include or require. Change this to include|require_once instead, it will only import the file if it hasn't already been imported. This will prevent errors occurring that indicate redefinition of functions, classes, constants, etc.

ajloun
09-04-2009, 01:43 PM
Went back to my file to check , and your are right i was useing include .. changed it to include Once .. and all worked fine .. Thank you Man One More Time .



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum