thelolcat
thelolcat

Reputation: 11545

Can "define" (a name of a built-in function) be used as a method name?

Can I use it as a method name in my classes?

It appears to be a function: http://www.php.net/manual/en/ref.misc.php But I see there listed some language constructs too, like die and exit.

Upvotes: 0

Views: 68

Answers (2)

CodeAngry
CodeAngry

Reputation: 12985

When you can... you can, when you can't... you'll know :)

See a list of reserved words here http://php.net/manual/en/reserved.php and avoid them. Any good PHP IDE will warn you when you attempt to illegal-name your methods.

There's another issue. If you forget $this->Method() and you just use Method() in your class, it will work as it's defined as a function. This will lead to freak and hard to find bugs.

Upvotes: 2

user229044
user229044

Reputation: 239301

<?php

class Test {
  function defined() {
    return "Yes you can";
  }
}

$x = new Test();
echo $x->defined();

Yes, you can. No, you shouldn't. Using the same name as built-in functions is never a good idea. The word defined has an established meaning in PHP, and nobody should have to think harder to figure out how your class is using (or abusing) the word in some specific context.

Upvotes: 2

Related Questions