Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts

    is there workaround to use same function

    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
    PHP Code:
    Fatal errorCannot 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..
    Last edited by ajloun; 09-04-2009 at 12:52 AM.

  • #2
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    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.
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by Zangeel View Post
    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.

    PHP Code:
    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 Code:
    <?php

    namespace MyProjectFuncLoads1
    {
        function 
    doFunc(array $a)
        {
            
    printf("\$a = %s\n"print_r($atrue));
        }
    }

    namespace MyProjectFuncLoads2
    {
        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();
        
    MyProjectFuncLoads1doFunc($a);
        
    MyProjectFuncLoads2doFunc($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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    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

    PHP Code:
    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($badword01) == "[" && substr($badword, ($len 1), 1) == "]") {
                    
    $content preg_replace("@".substr($badword1, ($len 2))."@i"substr($rep0, (strlen($rep) - 2)), $content);
                    continue;
                }

                
    $badwordpreg=preg_split('//'$badword, -1PREG_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;
        }


  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    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 ..
    PHP Code:
    if (!function_exists("validate"))
    {

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

    i just Coudt do it ..

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    ajloun (09-04-2009)

  • #8
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    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 .


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •