0

I'd like to write a File-Class, but there is already a function in PHP which is called file().

Am I allowed to name my class like the function?

<?
    class file{
        public function __construct(){
            /* DO_STH */
        }
    }

    $a = new file();
?>

It works without a problem (PHP 5.4). Do you know a reason for not doing it?

2
  • Keyword new helps the interpretet for call inference. Commented Sep 28, 2013 at 16:57
  • You may sometimes find function Foo() { return new Foo; } Foo()->bar();, although now that PHP supports (new Foo())->bar() it's not as useful. Commented Sep 28, 2013 at 17:08

1 Answer 1

2

Functions and classes exist in separate namespaces and their syntax is unambiguous. There's no problem for a class to use the same name as a function.

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

Comments

Your Answer

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