Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/72505-private-subclass-constructor/
Share on other sites

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?

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.

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.

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.
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 3
The 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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.