Monday, 5 September 2016

php - 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.



class ParentBase 
{
public static function createObject()

{
echo get_class() . "
"; // prints ParentBase
echo get_called_class() . "
"; // 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


Answer



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() . "
";

echo get_called_class() . "
";
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.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...