7

I'm just curious as to if creating an object on the fly is possible in PHP. I thought I'd seen it done before. Of course I can just assign it to a variable but just wondering if this possible.

new className()->someMethod();

Of course this throws a syntax error, so obviously it's not done like that (if it's even possible). Should I just assign it to a variable, as I really have no problems with doing that I was just curious?


Just some further details. Static methods aren't really an option as the class I was trying to do this for was PHPs ReflectionMethod class.

4
  • 8
    Have you tried (new className())->someMethod();? Commented Jul 4, 2011 at 5:07
  • Yeah it doesn't work like that either jerluc. Commented Jul 4, 2011 at 5:12
  • possible duplicate of calling a method of an object at instance creation Commented Jul 4, 2011 at 7:21
  • Yes thanks Gordon, couldn't find anything when I was looking, my title is much better. ;) Commented Jul 4, 2011 at 16:08

5 Answers 5

13

this usage is typical in Java but is not available before PHP 5.3. And now it is a new feature in PHP 5.4. Pls check PHP 5.4 new features. And the usage should be:

(new Foo)->bar()
Sign up to request clarification or add additional context in comments.

2 Comments

Amazing! No need to write create() for every single class!
The link is broken.
1

This only works if you are using a singleton-pattern for instanciating the object. If you are not aware of how to implement the singleton-pattern, you'll have to search around the web. But this way it would work:

className::getInstance()->someMethod();

EDIT

As stated by zerkms a factory-method would also be possible:

class ReflectionFactory
{
    public static function factory($arguments)
    {
        return new ReflectionClass($arguments);
    }
}

// Then in your code for example
ReflectionFactory::factory()->getConstants();

6 Comments

+1 This would be the preferred method. But this is only true under the assumption that a singleton is appropriate to the OP's situation.
This method is great, but not possible since I'm trying to do this for PHPs ReflectionMethod class.
@jerluc: if it is not appropriate - then the factory could replace it ;-)
-1 because a Singleton is almost never a good solution and it certainly isnt the right solution here because the OP is not looking to get the same instance on each request, nor is he looking for global access to said instance.
I'm going to go ahead and select this as the only decent way of doing this. The factory way is handy. However it is not the answer to my question mainly because I don't think my question can be answered correctly yet. Oh well, thanks for the help guys.
|
0

Currently in php it is not possible to instantiate new object and invoke its methods in one expression.

So obviously you need to assign it to some variable before.

1 Comment

Oh well it was worth a shot. :)
0

You could use a static method on a class without saving an instance of the class to a variable.

class testClass{
    public static function staticMethod(){
        echo "Static method";
    }
}

testClass::staticMethod();

Comments

-1

I dont know why PHP dont allow chaining of constructors. If you really need to do this try something like below with necessary tweaks.

<?php
class A {
  public function b()
  {
    echo "a->B is called";
  }
  public static function factory()
  {
    return new self;
  }

}
$a  = A::factory()->b();
?>

Comments

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.