I’m jealous of Ruby with their use of “new” as a method. Is it possible to achieve this in PHP 5.3 with the use of namespaces?
class Foo
{
public function new()
{
echo 'Hello';
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No. Like already pointed out elsewhere,
newis a reserved keyword. Trying to use it as a method name will result in a Parse error: “syntax error, unexpectedT_NEW, expectingT_STRING“. Namespaces will not help, because thenewkeyword applies to any namespace. The only way around this would be by means of a virtual method, e.g.But I would discourage this. All magic methods are slower than direct invocation of methods and if the simple rule is there may be no method name
new, then just be it. Use a synonym.