2

I know that default is a reserved keyword in PHP. I came up to this weird situation where I wanted to name my class Default, but it seems whenever I try to create it in any place in code my whole page turns blank and PHP is not showing any error. It's just really confusing and I spent over 20 minutes searching for the problem in my code. Because i'm annoyed by that, I want to know what's wrong with this.

(I've tried even with different namespaces, but still the same effect, for example:)

new \SomeNamespace\Default();

Can someone explain for me why this is happening and there isn't any error showing up? Is this just a PHP flaw that we have to live with?

11
  • 3
    my whole page turns blank and PHP is not showing any error - Enable display_errors Commented May 26, 2015 at 20:30
  • 3
    This is a syntax error, so there's no output. Check the PHP error log. Commented May 26, 2015 at 20:33
  • 4
    Just out of curiocity, why do you want to name your class 'Default'? Apart from it being a keyword, what does it do? Shouldn't it be named 'DefaultSettings', 'DefaultController', 'DefaultCustomer' or whatever? Commented May 26, 2015 at 20:36
  • For some reason display_errors don't work for me in this case. I will see what I can find in error log. Commented May 26, 2015 at 20:37
  • try using print_r(error_get_last()); to see if it shows any error. Commented May 26, 2015 at 20:37

1 Answer 1

1

http://php.net/manual/en/reserved.keywords.php

These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion.

Simply no no no. It is not possible whatever your script outputs errors or not. Try to name your class with better meaningful noun.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, that's right... I will rename it to something else. I found an error in logs, so I will remember to check it more frequently. Thanks for help guys ;)
actually you can name a class method public function default(){}
Hi. Yes, you can do it now but from PHP 7.0+.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.