leonglass Posted October 9, 2007 Share Posted October 9, 2007 Does anyone know why I can't have a private constructor when I have a class that extends a class with a public constructor. Example: class a { public constructor(){...} } class b extends a { private constructor(){...} } I can see why the other way but don't understand the logic for disallowing this. Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/ Share on other sites More sharing options...
Barand Posted October 9, 2007 Share Posted October 9, 2007 If constructor is private, how are you going to call $b = new b(); Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-365643 Share on other sites More sharing options...
emehrkay Posted October 9, 2007 Share Posted October 9, 2007 What are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-365674 Share on other sites More sharing options...
leonglass Posted October 10, 2007 Author Share Posted October 10, 2007 Singleton pattern for class b but extending from class a. static function getinstance() { if(!$instance) { $instance = new b(); } return $instance; } declared in class b would be how I would call it. It works making the contrustor public but I was just wondering why I was getting an error when class b constructor is made private. Would it be different if the class were final? Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-365988 Share on other sites More sharing options...
emehrkay Posted October 11, 2007 Share Posted October 11, 2007 http://en.wikipedia.org/wiki/Singleton_pattern#PHP_5 Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-366612 Share on other sites More sharing options...
leonglass Posted October 11, 2007 Author Share Posted October 11, 2007 http://en.wikipedia.org/wiki/Singleton_pattern#PHP_5 Been there read that pretty much what I have. What about my question? Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-366848 Share on other sites More sharing options...
emehrkay Posted October 11, 2007 Share Posted October 11, 2007 Does your code automatically error when you parse it? Do you have a "getInstance" method that returns self::__construct() ? Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-367125 Share on other sites More sharing options...
leonglass Posted October 11, 2007 Author Share Posted October 11, 2007 Does your code automatically error when you parse it? Do you have a "getInstance" method that returns self::__construct() ? Yes it does error when parsed. I'm not so interested in the singleton itself I am more interested in why I can't override a public constructor with a private one. I am trying to remember my OO theory and seem to remember that you shouldn't be able to make an overridden method more restricted. I didn't think that this applied to constructors which are treated slightly differently as they are only called in certain circumstances (construction). I admit my experience with this is from Java but from what I can remember this layout would have been ok. If I am unable to create a private constructor in a subclass this means that any class I want to create like this must be the base class and any inheritance would have to be displaced to interfaces. Is this how I would have to go about it with php. Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-367194 Share on other sites More sharing options...
coder_ Posted October 11, 2007 Share Posted October 11, 2007 Correct me if i am wrong, but i think you are trying to avoid more inheritance by making a subclass's constructor private. Why don't just define that class "final"??? And answer to one of your previous posts. No, you wont get an error if you declare class final and class's constructor public. Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-367226 Share on other sites More sharing options...
leonglass Posted October 11, 2007 Author Share Posted October 11, 2007 Correct me if i am wrong, but i think you are trying to avoid more inheritance by making a subclass's constructor private. Why don't just define that class "final"??? And answer to one of your previous posts. No, you wont get an error if you declare class final and class's constructor public. What I was trying to do was implement a singleton pattern on a class that was already inheriting from another class. I have just made the constructor public to avoid the problem but was just wondering as to why the error occurred. My question about using the final keyword was related to allowing the private constructor as subclassing cannot happen for a final class. Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-367260 Share on other sites More sharing options...
coder_ Posted October 11, 2007 Share Posted October 11, 2007 And what error do you get??? Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-367266 Share on other sites More sharing options...
leonglass Posted October 11, 2007 Author Share Posted October 11, 2007 Fatal error: Access level to system::__construct() must be public (as in class database) in /var/www/localhost/htdocs/gamvc/includes/system.php on line 3The structure is class Object class database extends Object class system extends database system has a private constructor. As I said making the system constructor public fixes it. Also tried making system final and no difference. Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-367332 Share on other sites More sharing options...
emehrkay Posted October 11, 2007 Share Posted October 11, 2007 I just tried it and it tells me that b has to be public, I do not know why. <?php class a{ public $_test = 'not set'; public function __construct(){ } } class b extends a{ private static $instance; public function __construct(){ $this->_test = 'hey hey hey'; } public static function getInstance() { if (self::$instance === null) { self::$instance = new b(); } return self::$instance; } } $x = b::getInstance(); echo $x->_test; ?> but do you need to extend? Are you using the "has a" and "is a" correctly? I got this from another site: All of the pain caused by inheritance can be traced back to the fact that inheritance forces ‘is-a’ rather than ‘has-a’ relationships. If class R2Unit extends Droid, then a R2Unit is-a Droid. If class Jedi contains an instance variable of type Lightsabre, then a Jedi has-a Lightsabre. The difference between is-a and has-a relationships is well known and a fundamental part of OOAD, but what is less well known is that almost every is-a relationship would be better off re-articulated as a has-a relationship. Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-367344 Share on other sites More sharing options...
leonglass Posted October 12, 2007 Author Share Posted October 12, 2007 .. but do you need to extend? Are you using the "has a" and "is a" correctly? I got this from another site: ... Probably not no and I have changed things around now. I was just interested in why the private constructor was not allowed.. Quote Link to comment https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/#findComment-367672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.