Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create class instance from within static method

As the title says, I'm wanting to create an instance of a class from within a static method of the same class. I've figured out so far is that I can by doing something like this:

class Foo{   public $val;   public static function bar($val){   $inst = new Foo;   $inst->val = $val;   return $inst;  }  } 

Which therefore lets me do this.

$obj = Foo::bar("some variable"); 

Which is great.

So now the questions. Is there an easier way of doing this that I'm not aware of, or any shortcuts to achieving the same result? Are there any advantages or disadvantages of creating an instance in this fashion?

Thanks.

like image 516
daniel Avatar asked Jan 11 '11 14:01

daniel


People also ask

Can you create instances of classes inside a static method?

Since they belong to the class, so they can be called to without creating the object of the class. Important Points: Static method(s) are associated with the class in which they reside i.e. they are called without creating an instance of the class i.e ClassName.

How do you create an instance of a static class?

In C#, one is allowed to create a static class, by using static keyword. A static class can only contain static data members, static methods, and a static constructor.It is not allowed to create objects of the static class. Static classes are sealed, means you cannot inherit a static class from another class.

Can we instantiate object in static method?

Yes, it is perfectly ok to instantiate objects inside static methods.

Can we create instance of static class in Java?

An instance of the static nested class can be created without creating an instance of its outer class. The static and non-static members of an outer class can be accessed by an inner class. The static members of the outer class can be accessed only by the static class.


2 Answers

They way you're doing it is fine. There are a few other things that can make your life easier that you can do as well.

  1. Don't hardcode the class name. If you're on 5.3+, use the keyword static. That way, if you extend the class, the new function can instantiate that one as well:

    public static function bar($var) {     $obj = new static();     $obj->var = $var;     return $obj; } 

    Then you can use it in any extending class without needing to override anything.

  2. Figure out if $var should be passed in through a constructor rather than set after construction. If the object depends upon it, you should require it.

    public function __construct($var) {     $this->var = $var; } 

    That way you can't instantiate the object without setting the variable.

  3. Enforce the instantiation of the class through the static method. If you're doing anything in there that you need to do, then make the constructor either protected or private. That way, someone can't bypass the static method.

    protected function __construct() {} private function __construct() {} 

I hope that helps...

Edit: Based on your comment above, it sounds to me like you're trying to implement the Singleton Design Pattern. There's tons of information out there about why it's not a great idea and the bad things it may do. It has uses as well.

But there are a few other patterns that may be of use to you depending on what you're doing exactly.

  • You can use the Factory Method if you're trying to create different objects using the same steps.
  • If all of the objects start off the same and then are customized, you could use the Prototype Pattern.
  • You could use an Object Pool if it's particularly expensive to create your object.

But one thing to consider, is that in PHP objects are pretty light weight. Don't try to avoid creating a new object just for that overhead. Avoid doing heavy things like database queries or filesystem accesses multiple times. But don't worry about calling new Foo() unless foo's constructor is particularly heavy...

like image 127
ircmaxell Avatar answered Oct 08 '22 06:10

ircmaxell


This looks like a simple factory method pattern.

You have a nice advantage: suppose that in the future you want to start using a different implementation (but that does the same thing). Using a factory you can change all the objects that are created in many places of a complex system simply by changing the creator method. Note that this would work easier if you used an external class (as is in the first link below).

Keeping it as you have now, you can also subclass this class and override the method to create a more complex object. I don't think this is what you want to achieve in here.

Anyway, this is good to enable Test Driven Development, abstraction and lots of other good things.

links:

  1. Php patterns
  2. Factory method pattern on wikipedia
like image 43
Pedro Loureiro Avatar answered Oct 08 '22 04:10

Pedro Loureiro