?
Solved

Fatal error: Cannot redeclare stripslashes

Posted on 2011-09-15
8
Medium Priority
?
833 Views
Last Modified: 2012-06-21
I am looking for documentation that describes PHP OOP method names.  See the code and description for an interesting example.

The code in the snippet below is posted on my server at http://www.laprbass.com/RAY_temp_adamssap.php
Outputs
Fatal error: Cannot redeclare StripSlashes() in /home/websitet/public_html/RAY_temp_adamssap.php on line 25

If you remove the code from line 20 to the end, the script runs correctly.

The apparent behavior shows that it is a fatal error to redeclare a function name outside of the class.  However the method name inside the class is somehow distinct from PHP function names.  This makes sense in that two different classes may have the same method names.  However it seems slightly counterintuitive to me and might make for awkward programming if one reused the built-in PHP function names to name methods.  It also seems as if there should be documentation about "function scope" like there is about "variable scope."

I understand variable scope.
http://php.net/manual/en/language.variables.scope.php

Can anyone please point me to the man page that describes "function scope?"

Thanks, ~Ray
<?php // RAY_temp_adamssap.php
error_reporting(E_ALL);

// A CLASS DEFINITION WITH A METHOD NAME THAT DUPLICATES A PHP FUNCTION
Class Thing
{
    function StripSlashes($str)
    {
        if(get_magic_quotes_gpc())
        {
            $str = stripslashes($str);
        }
        return $str;
    }
}

$x = new Thing;
$y = $x->stripSlashes('abcdefg');
var_dump($y);

// TRY TO DEFINE THE FUNCTION OUTSIDE OF THE CLASS CONTEXT
function StripSlashes($str)
{
    return TRUE;
}

Open in new window

0
Comment
Question by:Ray Paseur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 36542976
the problem is NOT your class method.  As a matter of fact, you can rename StripSlashes to StripSlashesR on both of your definitions and it should work as you expect. The problem is that on line 22 you are attempting to re-define the built-in stripslashes() function.

AFAIK, the functions are case insensentive (or at least they have been in the windows AND linux environments where I've run php).
0
 
LVL 111

Author Comment

by:Ray Paseur
ID: 36543040
Thanks, hielo.  I understand the fatal error; this is only demonstration code.  I am wanting to find the PHP man page reference that tells us (1) it is not OK to redeclare a function, and (2) that inside a class, a method may redeclare a function name.  I believe the principle at work here is something like this:

stripslashes() is not the same as thing->stripslashes()

I expect that I cannot redeclare thing->stripslashes() and that I cannot have two classes with the same name, etc.  I'm just looking for the documentation reference.

Best regards, ~Ray
0
 
LVL 111

Author Comment

by:Ray Paseur
ID: 36543056
PS: functions are case insensentive...

Right. I found this: "Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration."
http://php.net/manual/en/functions.user-defined.php
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 82

Expert Comment

by:hielo
ID: 36543194
>> (1) it is not OK to redeclare a function,
PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.
http://www.php.net/manual/en/functions.user-defined.php

(Right after ex 3)
0
 
LVL 111

Author Comment

by:Ray Paseur
ID: 36543866
Yes, it was the statement right before that.  "All functions and classes in PHP have the global scope..." So it seems kind of counterintuitive that I can write...

function StripSlashes($str)

... inside a class, but not outside a class, where it collides with the predefined PHP function of the same name.  I understand the concept; I'm just looking for the best example of the documentation that supports it.

Thanks and regards, ~Ray
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 36543986
>> (2) that inside a class, a method may redeclare a function name
Well, you are not really "redeclaring" a "global" function (which is what you meant). The global function is "tied/belongs to" to the global scope, where as the method is "tied/belongs to" to the class.  

Perhaps a "better" way to express that idea is that you cannot redefine/redeclare a function within the same scope. Once declared it's "final".

...It is not possible to subtract from a class, that is, to undefine any existing functions or variables
http://www.php.net/manual/en/keyword.extends.php

If you are trying to explain this concept to someone, perhaps you can use the file system analogy given at:
http://www.php.net/manual/en/keyword.class.php

starting with "Both, $cart and $another_cart..." (although they are trying to explain different ideas from what you are after, the analogy applies).
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 2000 total points
ID: 36544034
"All functions and classes in PHP have the global scope..."
>> So it seems kind of counterintuitive that I can write...
>>function StripSlashes($str)
>>... inside a class, but not outside a class

I think you are reading TOO fast. It clearly states "CLASSES .... have global scope", not their methods.  The method belongs to the CLASS, so it is in a different scope.  In other words, for you to call StripSlashes of Thing object, you need to "prefix" the method with the Thing object (or an instance of the Thing object depending on your method - static, public).
0
 
LVL 111

Author Comment

by:Ray Paseur
ID: 36544056
Thanks.  I think the $cart and $another_cart makes the case pretty well.  Appreciate your help.  All the best, ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question