4

I want to ask if it is good practice to make a class create instances of self or not, for example I have a class:

class MyClass {
    private $data = "";
    private $children = array();

    public function add() {
        $x = new MyClass();
        // do something
        // $x->some_method('');
        $this->children[] = $x;
    }

    public function children() {
        return $this->children;
    }
}

That way I can use some Tree functionality, but is it alright or must I do something differently?

For example creating a parent class A, with children of class B and using a multi-dimensional array in class A to store children?

Maybe someone can suggest another method doing this?

If someone had experience with this could he please provide pros and cons he noticed.

1
  • 2
    You can use $x = new self(); Commented Jul 11, 2012 at 18:24

3 Answers 3

4

Is it good practice to make a class create instances of self?

it depends on why you're doing it.

if you're making the class create an instance of itself because you're using it as a singleton, you can get into hot water with some people about the singleton design pattern in general.

on the other hand, I find the self-instantiation for the purpose of the factory design pattern to be exquisite. And I do recommend doing that for that particular case.

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

1 Comment

Yes, currently I have a task to do this in a framework CodeIgniter, which is using singleton pattern. Maybe you can tell more about what would be better to use if we are working with singletons? As to why I am doing this, as I said - to make a tree structure.
1

In general, I would not. The only time I could see this being appropriate would be if you were using it in a Singleton, which does not appear to be the case.

Here an article about constructor best practices. It is pretty intense, but packed full of good information!

3 Comments

As I commented on Kristian's answer, I really use it in a singleton framework, but as a Library it does not extend anything an can exist in any way I wish. Thank you for the link!
You're welcome! It really is good stuff, something to read in your down time...I don't see any reason why this would be 'bad' since you are using it in a 'true' Singleton pattern.
I've used this technique in geographic libraries. Methods on a coordinate class are used to create instances of that class. Each method creates the coordinate using different input - lat/long, easting/northing, UTM object etc. but normalises the input to a single internal data structure. Basically, one size constructor does not fit all; it allows a new object to be constructed in different ways, without the need to use external factories to do it.
1

I don't think its a hard and fast rule either way. It depends on the problem space you're trying to resolve.

That is, if you're creating a structure like a Tree, and that structure contains nodes that are of type Tree, then it might be perfectly reasonable for trees to create instances of itself depending upon the requirements of the Tree in the subject application.

You also see something roughly similar to your example in the Singleton design pattern, where you want to create exactly one universal instance of an object.

So, I think the answer is in the suitability of the solution to the problem. Hope that makes sense.

2 Comments

Could you please extend your answer, the part about similarity with singletons? I want to understand, will it be a singleton if it has multiple objects as children of a parent object? I mean in the global scope it will be a singleton, but in a parent object's scope will it be singleton as well, if it uses instances of one class as children, to which you can reference by name and, what I want to achieve - by name?
I'll try :) My understanding is that a Singleton design pattern guarantees that, no matter how many times you may call a constructor for an object, the implementation guarantees that only one object is ever actually created and returned. In your sample, however, it appears to me that you want/need distinct instances of MyClass to populate the tree you are discussing - otherwise, your tree would be nothing more than MyClass with an array of self-referencing children.

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.