Peter Ajtai
Peter Ajtai

Reputation: 57685

Can I call a private child constructor from a base factory method?

I'd like to implement the following using a private constructor.

The problem is that get_class() returns ParentBase; eventhough; get_called_class() returns ChildClass.

How can I have __construct() be called from the calling class context instead of the base class context?

There will be many child classes, so I only want one shared factory method, and I also want to make sure a child cannot be extended (so that it cannot be created with the new keyword).

Seems like there should be a way of making ChildClass::createObject() work with a private ChildClass constructor and a public ParentBase factory method.

<?php
class ParentBase 
{
    public static function createObject() 
    {
        echo get_class() . "<br/>";        // prints ParentBase
        echo get_called_class() . "<br/>"; // prints ChildClass
        return new static();
    }
}

class ChildClass extends ParentBase
{
    private $greeting = "bye";

    private function __construct()
    {
        $this->greeting = "hi";
    }

    public function greet()
    {
        echo $this->greeting;
    }
}

$child = ChildClass::createObject();
$child->greet();

The output from the above is:

ParentBase
ChildClass
Fatal error: Call to private ChildClass::__construct() from context 'ParentBase' 

Protected contstructor works: http://codepad.viper-7.com/sCgJwA

Private constructor doesn't: http://codepad.viper-7.com/YBs7Iz

Upvotes: 1

Views: 688

Answers (3)

Peter Ajtai
Peter Ajtai

Reputation: 57685

If this is PHP 5.4......

After playing around with some other things and reading up on PHP OOP. It looks like Traits can do this.

I like the use Trait notataion, which in this case makes it obvious that you should use the Factory to instantiate the class.

Using a Trait is advantageous in that it the Factory Trait can be shared across multiple Class hierarchies that do not have common lineages:

<?php

// NOTE: traits are only avaialable in ** PHP 5.4 **
trait Factory 
{
    public static function createObject() 
    {
        echo get_class() . "<br/>";        // prints ChildClass
        echo get_called_class() . "<br/>"; // prints ChildClass
        return new static();
    }
}

class ChildClass
{
    use Factory;

    private $greeting = "bye";

    private function __construct()
    {
        $this->greeting = "hi";
    }

    public function greet()
    {
        echo $this->greeting;
    }
}

$child = ChildClass::createObject();
$child->greet();

Working Code Example

Upvotes: 0

Starx
Starx

Reputation: 78971

That is an expected behavior createObject(); is a function of ParentBase, So it will return ParentBase from get_class() but, it was called from ChildClass So, it will return ChildClass from get_called_class().

And about the constructor, since the constructor is assigned as private, you restrict the object creation from within the class only. By making it protected, now Parent Class can create the object of ChildClass

Probable solution would be to override, the createObject() class in the ChildClass itself.

class ChildClass extends ParentBase
{
    public static function createObject() 
    {
        echo get_class() . "<br/>";
        echo get_called_class() . "<br/>";
        return new static();
    }
}

Or, you could make the constructor protected, then you will make the constructor accessible to parent classes and restrict any sub classes of child classes making it final, thus making it accessible from parent class only.

Upvotes: 1

Jacob Pollack
Jacob Pollack

Reputation: 3751

The child constructor must be protected or public to my knowledge. I ran into a similar issue for a different problem, I was attempting to access a private property.

But for some reason your question "Can I call a private child constructor from a base factory method?" does not reflect your code so I suggest you edit that as I am troubling myself over how to answer this.

Upvotes: 0

Related Questions